Search code examples
c#regexregex-negation

Regular expression with quantified part and negative lookahead


I need check if a string contains the pattern: starts with "A" followed by zero or more spaces and then anything but not "B".

So, the following must match: "A". "AX", "A X", "A ", "A XB"

The following strings must not match: "AB", "A B"

My naive attempt was A\s*(?!B), but it matches the undesirable "A B".


Solution

  • If you just need to get true or false, you may put the \s* into the lookahead:

    Regex.IsMatch(s, @"A(?!\s*B)")
    

    It finds A that has no 0+ whitespaces followed with B after it.

    See the regex demo.

    In your pattern, A\s*(?!B), the negative lookahead can be executed after any 0+ whitespaces, and once a whitespace not followed with B is found, a valid match is returned (that happens due to backtracking that is possible thanks to \s* quantified pattern).

    If you need to actually match the A and the whitespace after it, but if these whitespaces are not followed with B, use the pattern from my comment.

    (?>A\s*)(?!B)
    

    This pattern matches:

    • (?>A\s*) - an atomic group, matches A, then 0+ whitespaces with no backtracking into the group pattern allowed
    • (?!B) - no B after the spaces, or the whole match is failed.