1

The following regular expression will match everything that is between the expression Peter and Parker as long as Parker is found within 0-20 characters after Peter: 'Peter[\s\S]{{0,20}}Parker'

Now I want to match everything as long as Parker is within 0-20 characters except when there is a keyword that appears within this range. When the keyword appears, this should act as the upper range, so that instead of {0,20} we will have something like {0,keyword} for the range in which Parker should appear.

Example:

Let the keyword be "Hulk".

test1 = "My name is Peter, I like spiders very much Parker" -> Should not match as Parker is more than 20 characters away from Peter

test2 = "My name is Peter, Jason Hulk Parker" -> Should not match. Even though Parker is within the original 0-20 range, the keyword "Hulk" appears in this range and acts as the upper range of the regex.

test3 = "My name is Peter Jason Parker and I don't like the Hulk" -> Should match. Parker is within the 0-20 range and Hulk appears out of this range.

xxgaryxx
  • 335
  • 1
  • 7

0 Answers0