1

Here is string:

step into
1
2
step into
3
4
step out

when I run:

r = re.compile('step into[\s\S]*?step out').search(s).group()

I get:

step into
1
2
step into
3
4
step out

However, I want:

step into
3
4
step out

It seems I should make non-greedy regular search from right to left to get the right result. How can I do it with python?

jiligulu
  • 187
  • 6

1 Answers1

2

You could use re.findall with the following regex pattern:

\bstep into(?:(?!step into).)*?\bstep out\b

Python script:

inp = """step into
1
2
step into
3
4
step out"""
matches = re.findall(r'\bstep into(?:(?!step into).)*?\bstep out\b', inp, flags=re.DOTALL)
print(matches)

This prints:

['step into\n3\n4\nstep out']

Here is an explanation of the regex pattern:

\bstep into           match "step into"
(?:(?!step into).)*?  match any content, across newlines, so long as "step into"
                      is NOT encountered before seeing "step out"
\bstep out\b          match the first "step out" after "step into"
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360