Multiple regex options with C# Regex

前端 未结 5 1172
借酒劲吻你
借酒劲吻你 2021-01-03 23:28

Assume I have this:

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

But I also need to ignore white-spaces. So, I fo

相关标签:
5条回答
  • 2021-01-03 23:41

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

    For example...

    RegexOptions.Compiled | RegexOptions.IgnoreCase
    
    0 讨论(0)
  • 2021-01-03 23:52
    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.

    0 讨论(0)
  • 2021-01-03 23:55

    Use bitwise OR (|)

    Regex.Replace("aa cc bbbb",
                    "aa cc",
                    "",
                    RegexOptions.IgnoreCase | RegexOptions.IgnorePatterWhitespace); 
    
    0 讨论(0)
  • 2021-01-03 23:56

    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();
    
    0 讨论(0)
  • 2021-01-04 00:02

    According to MSDN:

    A bitwise OR combination of RegexOption enumeration values.

    So just use OPT_A | OPT_B

    0 讨论(0)
提交回复
热议问题