tags:

views:

49

answers:

3

Is it possible somehow to do a RegEx-replace with a calculation in the result? (in VS2010)

Such as:

Grid\.Row\=\"{[0-9]+}\"

to

Grid.Row="eval(int(\1) + 1)"
A: 

You could always use capturing to retrieve any values you need for your calculation and then perform a RegEx Replace with a new RegEx that's constructed from you're equation and any values you captured.

If the equation doesn't use anything from the input text, one RegEx would be sufficient. You'd simply construct it by concatenating the static portions together with the computed value(s).

colithium
A: 

You can use a MatchEvaluator do achieve this, like

 String s = Regex.Replace("1239", @"\d", m => (Int32.Parse(m.ToString()) + 1).ToString());

Output: 23410

Edit: I just noticed... if you mean "using the VS2010 find-replace feature" and not "using C#", then the answer is "no", i am afraid.

Jens
A: 

Unfortunately, C# and .NET do not provide an eval method or equivalent. However, it is possible to either use a library for expression parsing (a quick google gave me this .NET Math Expression Parser) or write your own (which is actually pretty easy, check out the Shunting-yard Algorithm and Postfix Notation). Simply capture the group then output the group value to the library/method you have written.


Edit: I see now you want this for the VS2010 program. This is unachievable unless you write your own VS extension. You could always write a program to search and replace your code and feed the code into it, then replace it the original code with its output.

Callum Rogers