简体   繁体   中英

Combining Positive Lookahead and Negative Lookahead - Regex

I'm writing a simple regex in postgres 9.5 using ~

I want to combine a positive lookahead and a negative lookahead. This is my Regex which is not working:

CHANGE.+(?=SHOES.+(?!NIKE))

Here is my DEMO and below is an example of my issue:

MATCH:

CHANGE THE SHOES TO REBOOK.

NOT MATCH:

CHANGE OF SHOES TO NIKE AIRS.

Here is a solution which only uses a single negative lookahead:

SELECT *
FROM yourTable
WHERE col ~ 'CHANGE.+SHOES(?!.*NIKE)';

If you want an exact answer to your question, see the response by @trincot.

The .+ after SHOES is greedy and so the (?! will only be checked at the end of the string where it is true (ie there is no NIKE there), and so there is no backtracking.

Instead do this:

CHANGE.+(?=SHOES(?!.*NIKE))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM