0

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.

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

1 Comment

Demo 2 is close but I require the substring i\s+need to be present in the string to take action, except for when it is preceded by specific keywords like (do|should) (this list might grow). So any other string like "hey how's it going today?" should not trigger a match. I've updated the question with this example to make it more clear.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.