user194076 user194076 - 1 year ago 127
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 Source

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.


string result = Regex.Replace("aa cc bbbb","aa|cc","",RegexOptions.IgnoreCase).Trim();
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download