-2

Hello I have a string like following

Some text here -   
~XY*901*000000001~MLC*~XY*901*000000005~MLC*~XY*901*0000000010~MLC* 
Some text here

I want to have it like -

Some text here 
~XY*901*000000001~MLC*~XY*901*000000002~MLC*~XY*901*000000003~MLC* 
Some text here 

Explanation: everything in between ~XY*901* and ~MLC* should be incremented by one starting from 1.

How to achieve it using Regex Match Evaluator?

Tim Schmelter
  • 450,073
  • 74
  • 686
  • 939
Ajay Suwalka
  • 549
  • 1
  • 7
  • 24

1 Answers1

1

A bit of lambada. Erhm, I mean lambda.

var input = @"Some text here -
~XY*901*000000001~MLC*~XY*901*000000005~MLC*~XY*901*0000000010~MLC*
More text here";

var reg = new System.Text.RegularExpressions.Regex(@"(~XY\*901\*)[0-9]+(~MLC\*)");

int i = 1;
var result = reg.Replace(input, m => m.Groups[1] + i++.ToString("D9") + m.Groups[2]);

Console.WriteLine(result);

The regex Replace loops through the matches of the regular expression. While increasing the integer.

Output:

Some text here -
~XY*901*000000001~MLC*~XY*901*000000002~MLC*~XY*901*000000003~MLC*
More text here 
LukStorms
  • 28,916
  • 5
  • 31
  • 45