python - Get string between two sub strings with limitation -
i need finding substring using regex, starting example:
given following string:
test_str = "start: 1111 kill 22:22 start: 3333 end"
i extract string between start , end doesn't involve kill:
wanted_result = (start: 3333 end)
note: need matches of start blablab end don't have kill between them
several tries failed, latest one:
pattern = re.compile(r'start:(.+?)(([^kill])end)',flags = re.dotall) results = pattern.findall(test_str)
which results in different result:
result = (' 1111 kill 22:22 start: 3333', ' end', ' end')
you need use negative lookahead based regex.
pattern = re.compile(r'start:(?:(?!kill).)*?end',flags = re.dotall)
(?:(?!kill).)*?
checking before match character. checks character going matched must not start of substring kill
.
example:
>>> import re >>> test_str = "start: 1111 kill 22:22 start: 3333 end" >>> pattern = re.compile(r'start:(?:(?!kill).)*?end',flags = re.dotall) >>> pattern.findall(test_str) ['start: 3333 end']
Comments
Post a Comment