Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# how to use Regex.replace, to replace all the pattern matches with the target string

Tags:

c#

regex

Yes, I want to search and replace all the occurrences of the pattern and replace them with a target string. I am trying to use Regex.Replace(src, pattern, target), is there a flag like \g to put in pattern to make it work, or what?

like image 748
Rn2dy Avatar asked Sep 23 '11 19:09

Rn2dy


1 Answers

There is no \g or concept of a global search, as the .NET Regex class is global by default. In other words, it should just work, assuming you've written your regular expression properly.

You might want to test it in Regex Hero and then once you've got it working click the .NET button at the top to get the code with properly escaped strings.

like image 77
Steve Wortham Avatar answered Nov 06 '22 01:11

Steve Wortham