ignore case sensitive in regex.replace?

13,073

Solution 1

You may try:

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

Solution 2

You simply pass the option RegexOptions.IgnoreCase like so:

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

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

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

A list of the available options you can set for regexes is available at the RegexOptions documentation page.

Solution 3

There's a Regex.Replace overload with RegexOptions. Those options include an IgnoreCase value.

Share:
13,073
Khalid Omar
Author by

Khalid Omar

Updated on June 08, 2022

Comments

  • Khalid Omar
    Khalid Omar almost 2 years

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

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

    textToReplace may be "welcome" or "client" or anything.

    I want to ignore case for textToReplace so that "welcome" and "Welcome" both match.

    How can I do this?

  • petro.sidlovskyy
    petro.sidlovskyy about 13 years
    Sorry, you was first :) so +1 to your post.