简体   繁体   中英

Regex match a substring if that substring is not preceded by a specific string and ignore the whole string?

Trying a few things with negative and positive look-behind, can't really get exactly what I want. Went through this SO question as well: Regex match a substring if that substring is not preceded by a specific string?

What I want is just a "yes this entire string should be considered" and a "no, ignore this entire string", because of these substring matches. The post above will help me match the substring, but if the negative words precede the substring, it's still a match, you can see my tests here: https://regex101.com/r/aqn1gO/2

What I'm trying to do is have a regex match for the substring i need , but ignore cases where it's not a request, but more a question. The examples are:

  1. i need you to do this
  2. hey i need this by tomorrow
  3. hey do i need this in the deliverable?
  4. hey should i need to do this?
  5. how are you doing today?

Where 1. and 2. should match, but 3. , 4. , and 5. should not, even though there's an i need in there.

One approach might be then to negate those with do or should followed by any chars and a must positive lookahead with i need :

^(?=.*i need.*)(?!.*should.*i need.*|.*do.*i need.*).*$

Demo

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