Jeff Reed
Jeff Reed

Reputation: 231

Regex replace with preservation

I want to replace a string in Visual Basic .NET, More specifically integers in a VB.NET script.

I'm using this method to recognize integers:

[^"](,|=|\/|\+|\-|\(|) (\d+)(\)|)[^"]

As you can see in the following Rubular test, it identifies normal integers and doesn't include the ones found in names, etc. Though I still have some problems identifying them.

Here's the test: http://rubular.com/r/q019lCUS45


Basically, what I want to do is add CInt(\d+) to all integers in a string of Visual Basic.NET script. So that it looks like this before:

Dim I As Integer = 0

And like this after:

Dim I As Integer = CInt(0)

Rubular

Upvotes: 0

Views: 116

Answers (1)

Javier Diaz
Javier Diaz

Reputation: 1830

Why aren't you using word boundaries? It removes for you the "numbers" that are surrounded by letters.

DEMO

/\b(\d+)\b/CInt(\1) With this regex, it'll search all numbers and replace them by CInt(NUMBER)

Upvotes: 1

Related Questions