Ignore case sensitive in regex.replace?

I have this code to search in a string and replace text with another text:

Regex regexText = new Regex(textToReplace);
retval = regexText.Replace(retval, Newtext);

textToReplace maybe "welcome" or "customer" or something else.

I want to ignore case for textToReplacematch greetings and greetings.

How can i do this?

+5
source share
3 answers

You can try:

Regex regexText = new Regex(textToReplace, RegexOptions.IgnoreCase);
+16
source

You simply pass the option RegexOptions.IgnoreCaseas follows:

Regex regexText = new Regex(textToReplace, RegexOptions.IgnoreCase);
retval = regexText.Replace(retval, Newtext);

Or, if you prefer, you can pass this option directly to the method Replace:

retval = Regex.Replace(retval, textToReplace, Newtext, RegexOptions.IgnoreCase);

, , RegexOptions.

+13

There Regex.Replace overload using RegexOptions . These parameters include the value of IgnoreCase.

+1
source

All Articles