Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

  • 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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading