1

I would like to create a regular expression that matches anything between two strings that does not contain a sequence in the middle.

Using this regular expression as an example: front.*(?!exclude)\.back$ I would like to match a string that is between front and back that does not contain exclude.

Test cases:

front_exclude.back - no match

front.back - match

front.helloworld.back - match

RyanCW
  • 1,012
  • 2
  • 10
  • 23

2 Answers2

0

Try this:

front(?:(?!exclude).)*\.back$
Josh Withee
  • 9,922
  • 3
  • 44
  • 62
  • Also could you provide an explanation as to why your approach worked? – RyanCW Nov 22 '17 at 03:53
  • Sure! Based on your original question, I'm assuming you understand how a negative look-ahead assertion works. The `(?:(?!exclude).)*` matches a sequence of characters such that the negative look-ahead assertion `(?!exclude)` is included before each character in the sequence. This makes sure that the string "exclude" does not appear anywhere in the sequence of characters. – Josh Withee Nov 22 '17 at 04:26
0
import re

txt = "The <Org> rain <Ort> in <Org> Spain <Ort>"

tag_start = "<Org>"
tag_end = "<Org>"
tag_exclude = "<(.*?)>"

reg_pattern = "(" + tag_start + "(?:(?!" + tag_exclude + ").)*\ " + tag_end + ")"

x = re.findall(reg_pattern, txt)
i_Hk
  • 1
  • 1