Search code examples
regexword-boundary

Regex - Issues with using Boundary to excluding words


In my authentification web site, I'm using regex to control a blacklist password. (example of blacklisted password : 12345678, 123456789, baseball, football)

I would like to add new regex rule (using boundary), which will exclude words (black listed password). I have read some similar questions on StackOverflow and tried to declare it with something like this:

^(?!\b12345678\b|\b123456789\b|\bbaseball\b|\bfootball\b|\bsuperman\b).*$

this regex doesn't match the words above, it's correct. For exemple "Baseball" with a letter, number or special character (before or after the "baseball") must match.

But "baseball!" doesn't match contrary to "!baseball". Can you give me some advices how to do it?


Solution

  • But "baseball!" doesn't match contrary to "!baseball"…

    baseball! doesn't match because your pattern doesn't allow baseball at the beginning (^ followed by a negative lookahead for baseball).
    !baseball in contrast matches because ! is placed at the beginning, and the negative lookahead is done only there, not aft.
    One could think of putting the .* at different places, but that will lead to nothing.
    Just include the anchors ^ $ in the lookahead:

    (?!^(12345678|123456789|baseball|football|superman)$)^.*$
    

    (in fact, we could even drop the initial ^).