I am trying to replace strings in a large amount of data and I was hoping to use regular expression for certain updates that I have to make to the text, I should point out that I am fairly new to regular expressions.
Show here is what I am trying to achieve. Within the text I am converting we have the following data {0} {1} {2}, this has to be converted to a new format so
{0} becomes %{ param0 } , {1} becomes %{ param1 }, {2} becomes %{ param2 }.
There is no limit on what the number could be, so far it has reached 16 and may go up in the future (It should be pointed out that the number is actually a string at this point). This is code I have so far but it only updates the value to {param0}. Also if the text contains more than one instance it only updates the first.
NewText = Regex.Replace(NewText , @"(?<=\{).*(?=\})", "param$0");
Thanks,
Brendy
You can write:
Regarding this:
What’s really happening is that if your string is
{0} {1} {2}, then your code will replace the0} {1} {2part withparam0} {1} {2, giving the end result{param0} {1} {2}. So it looks like it’s only updating the first instance, but really what it’s doing is updating all instances as though they were a single instance. This is because the.*notation swallows as much as it can while still allowing the regex to match.