user194076 user194076 - 2 months ago 29
C# Question

Multiple regex options with Regex.Replace

Assume I have this:

Regex.Replace("aa cc bbbb","aa cc","",RegexOptions.IgnoreCase);

But I also need to ignore whitespaces. So, I found an option IgnorePatternWhitespace, but how can I add several options to one regex.Replace? something like:

Regex.Replace("aa cc bbbb","aa cc","",RegexOptions.IgnoreCase + RegexOptions.IgnorePatterWhitespace);


UPDATE: Thanks for answers but this option does not seem to work: here's a test example:

Regex.Replace("aa cc bbbb","aacc","",RegexOptions.IgnoreCase | RegexOptions.IgnorePatterWhitespace);

Joe Joe
Answer

IgnorePatternWhitespace
Eliminates unescaped white space from the pattern and enables comments marked with #. However, the IgnorePatternWhitespace value does not affect or eliminate white space in character classes.

so:

string result = Regex.Replace("aa cc bbbb","aa|cc","",RegexOptions.IgnoreCase).Trim();