24

Assume I have this:

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

But I also need to ignore white-spaces. 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);
5
  • 1
    "Does not seem to work" isn't very informative. What kind of behavior specifically are you after? Commented Nov 15, 2011 at 20:20
  • This simply does not makes sense as a question - especially once you see what the accepted answer is, but even outside of that context! Commented Nov 15, 2011 at 20:50
  • use & instead of +, Regex.Replace("aa cc bbb", "aa cc","", RegexOptions.IgnorePatternWhitespace & RegexOptions.IgnoreCase) Commented Aug 19, 2014 at 3:47
  • vb.net version = Regex.Replace("aa cc bbb", "aa cc", String.Empty, RegexOptions.IgnorePatternWhitespace And RegexOptions.IgnoreCase) Commented Aug 19, 2014 at 3:48
  • | operator works well: C# working code demo ;). Commented Sep 20, 2017 at 6:40

5 Answers 5

92

Use bitwise OR (|)

Regex.Replace("aa cc bbbb",
                "aa cc",
                "",
                RegexOptions.IgnoreCase | RegexOptions.IgnorePatterWhitespace); 
Sign up to request clarification or add additional context in comments.

1 Comment

@user194076, if you are expecting regular expression "aacc" to match "aa cc" in "aa cc bbbb", you are interpreting the RegexOptions incorrectly. You have to use something like "aa\s?cc" to match that i.e. use \s? to match optional whitespace. Regex.Replace("aa cc bbbb","aacc","",RegexOptions.IgnoreCase |
13
Regex.Replace("aa cc bbbb","aa cc","",RegexOptions.IgnoreCase | RegexOptions.IgnorePatterWhitespace);

Use the | operator.

Edit :

You got it completely wrong. RegexOption.IgnorePatterWhitespace ignores the whitespace in the regex so that you can do :

string pattern = @"
^                # Beginning of The Line
\d+              # Match one to n number but at least one..
";

You however think that ingoring whitespace makes "aa cc bbbb" into "aaccbbbb" which is thankfully wrong.

Comments

8

According to MSDN:

A bitwise OR combination of RegexOption enumeration values.

So just use OPT_A | OPT_B

Comments

3

You can have as many RegexOptions as you like, just "OR" them with "|".

For example...

RegexOptions.Compiled | RegexOptions.IgnoreCase

Comments

2

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();

1 Comment

Please add a link to the original source you are quoting from.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.