I want to turn off the Global setting (as it is set default to be used on .NET) as what I am trying to do only needs to change the first part of a string, however there is the the chance that it will occur twice in the string, for example I wish for
C00000WPF0000SAND
to only change the first instance of 0’s.
if (Regex.IsMatch(text, "^[a-z]+00+", RegexOptions.IgnoreCase))
{
text = Regex.Replace(text, "0{1,}", "00*", RegexOptions.IgnoreCase);
}
This is how I am currently doing it, however this seems to take every single instance of the “00+” and change it but this messes things up.
You need to specify a match limit; the documentation for
Regex.Replace(string, string, RegexOptions)specifically states that it replaces all instances of the found pattern. There is no static overload ofRegex.Replacethat takes both a match limit and regex options, so you will have to construct a Regex object and then use theReplace(string, string, int)overload. Try this: