Search code examples
regexregex-group

Regular expression to match all 4XX numbers except 400 and 411


I have a bunch of 4XX status codes and I want to match all except 400 and 411.

What I currently have so far:

/4\d{2}(?!400|411)/g

Which according to regexer:

  • Matches a 4XX
  • Should specify a group that cannot match after the main expression (but it is here where my expression is failing).

Solution

  • The expression 4\d{2}(?!400|411) First matches a 4 and then 2 digits. After the matching, it asserts not 400 and 411 directly to the right.

    Instead, you can match 4 and first assert not 00 or 11 directly after it.

    4(?!00|11)\d{2}
    

    Or without partial word matches using word boundaries \b:

    \b4(?!00|11)\d{2}\b
    

    See a regex demo