Search code examples
regexregex-lookaroundsregex-look-ahead

Negative lookbehind not working as expected.How to prevent second group from capturing the string?


I want to reject strings that start with BT(uppercase and lowercase included)

Original regex:

^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z] [A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z]))))\s?[0-9][A-Za-z]{2})$

negative lookbehind regex:

(?<!([bB][tT]))(^([Gg][Ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z] [A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z]))))\s?[0-9][A-Za-z]{2})$)

but this regex is still accepting BT444CC The second group is capturing t how can i prevent this?


Solution

  • You do not need a lookbehind, you need a lookahead at the start of the string, (?![Bb][Tt]).

    Also, you need to enclose the whole pattern with a non-capturing group, else, the lookahead will only restrict the part before the first |.

    You can use

    ^(?![Bb][Tt])(?:[Gg][Ii][Rr] 0[Aa]{2}|([A-Za-z][0-9]{1,2}|([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2}|([A-Za-z][0-9][A-Za-z]|[A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))\s?[0-9][A-Za-z]{2})$
    

    See the regex demo.