Instead of using negative lookahead, sometimes it's easier to use the negation outside the regex at the hosting language level. In many languages, the boolean
complement operator is the unary !
.
So you can write something like this:
! str.hasMatch(/\.php$/)
Depending on language, you can also skip regex altogether and use something like (e.g. Java):
! str.endsWith(".php")
As for the problem with the original pattern itself:
(.*?)(?!\.php)$ // original pattern, doesn't work!
This matches, say, file.php
, because the (.*?)
can capture file.php
, and looking ahead, you can't match \.php
, but you can match a $
, so altogether it's a match! You may want to use look behind, or if it's not supported, you can lookahead at the start of the string.
^(?!.*\.php$).*$ // negative lookahead, works
This will match all strings that does not end with ".php"
using negative lookahead.
References
Related questions