Regular expressions: Find instances when the string does not equal a certain value

Another typical regex scenario: You’re looking through some data (like a JSON object) and are looking for when a value is actually being set. Let’s say our string contains:

"puttLength": null, 

Typically, you’d write a regular expression to capture the <em> tag by writing this:</em>

"puttLength": ((?!null).)*$

The use of ?! is the instruction to make a negative lookahead match, ensuring null does not exist before the line can be returned as a match. A terrific example / tool to test in your browser exists at https://www.regextester.com/15 .

Leave a Reply

Your email address will not be published. Required fields are marked *