2
Answers

Regex.Replace() - Is it possible to increment/decrement group values in-line?

Ask a question
Kevin Burt

Kevin Burt

16y
9.3k
1

Hello.

Does anyone know if it is possible to increment/decrement values in a Regex Group?

For example, the following code demonstrates capturing two groups called 'value1' and 'value2', and I would like to replace these value with (value1+1) and (value2-1) respectively.

string pattern = @"(?<value1>\d{1,2})/(?<value2>\d{1,2})";

string result = Regex.Replace(myString, patten, @"${value1}+1/${value2}-1");

I'm basically looking to manipulate multiple numeric values on a multi-line chunk of text.

Any advice would be much appreciated.


Answers (2)