1

I am trying to find the occurrence of various terms that are not preceded by another group of terms. Normally if I would have a single term in the preceding group I could use a negative lookbehind, but at Python has a zero-width assumption this doesn't seem to be the case. The only solution I see is to run two regexes, one for the presence of what I am looking for and another to confirm the non-existence of the preceding group terms. There must be a more elegant and efficient way of doing this. Can anyone help?

The test sentence is:

10 day trip excludes flights

The regex that assures that it isn't matched due to the word 'flights' being preceded by 'excludes' is the following:

(?:without|not including|doesn\'?t include|exclud(?:es|ing))\s*(?:flights?(?:\s+tickets)?|airfare|airline tickets?)

However I want to assure that certain text is included. I can confirm this with the following:

(?:flights?(?:\s+tickets)?|airfare|airline tickets?)

So this would match 'including ticket' and 'and airfare' but not 'without flight tickets'

Some examples of matching strings are:

including flights
includes flights
plus flights
flights are included
including airfare
and airfare

Some examples of non-matching strings are:

not including flights
flights are not included
excluding flights
without airfare
5
  • post some examples for matching and non matching strings. Commented Aug 21, 2014 at 16:29
  • I've added some matching and non-matching strings Commented Aug 21, 2014 at 16:45
  • you mean this regex101.com/r/kA0mL3/1 ? Commented Aug 21, 2014 at 17:04
  • That's the base. However that matches anything not preceded by those particular terms. You can see an example here: regex101.com/r/kA0mL3/4 Commented Aug 21, 2014 at 18:19
  • How about this regex101.com/r/kA0mL3/5 ? Commented Aug 21, 2014 at 18:36

1 Answer 1

2

You could try the below regex,

^(?=.*?(?:flights|airfare))(?:(?!without|not includ(?:ing|ed)|doesn\'?t include|exclud(?:es|ing)).)*$

DEMO

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

1 Comment

That's awesome, I didn't think of using a look forward.

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.