A regular expression for matching on one part of a string, not on another part

(appendCsrfToken).+(\.do\?\w)
line matches like ...
document.forms[0].action = appendCsrfToken("search.do?lname=Smith");

I would like to find lines that have a .do? part .do? but do not have appendCsrfToken . For instance...
document.forms[0].action = "search.do?lname=Smith";

I thought the following negation would work, but I don't get any matches when I test it
(^appendCsrfToken).+(\.do\?\w)

How to cancel appendCsrfToken correctly to get the match I'm looking for?

+4
source share
2 answers

Using ^ for negation only works in character classes. You need a lookahead . The easiest way is to look from the beginning of the line ( ^ ) all the way to appendCsrfToken with a negative look. If this works, then go ahead and match do? :

 ^(?!.*appendCsrfToken).*(\.do\?\w) 

Demo version

+4
source

You can also use a negative lookbehind. Here is the solution

 ^(?<!.*appendCsrfToken).*(\\.do\\?\\w) 
0
source

Source: https://habr.com/ru/post/1499207/


All Articles