7

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?

3 Answers 3

20

You may try:

Regex regexText = new Regex(textToReplace, RegexOptions.IgnoreCase);
Sign up to request clarification or add additional context in comments.

Comments

15

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.

1 Comment

Sorry, you was first :) so +1 to your post.
1

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.