Reputation: 31
I'm trying to match until I hit a pattern("this" ignoring any white spaces between start of line and pattern) or until the end of a string in a paragraph by using:
r'.*?(?=^[^\S\n]*this|$)'
This regex string works fine if my string is only one line($ matches an end of line). However I could not find the regex to match a end of string, so is there a clean way around this? The following is my code:
import re
a_str="""\
paragraph starts here
another line
this line may or may not exist"""
a_match = re.compile(r'.*?(?=^[^\S\n]*this|$)', re.MULTILINE|re.DOTALL).match(a_str)
EDIT:
Expected output:
"paragraph starts here\nanother line"
Upvotes: 3
Views: 1978
Reputation: 151
It's now possible to denote the very end of string in multi-line mode with '\Z'.
Ref: https://docs.python.org/3.8/library/re.html
Upvotes: 3
Reputation: 31
Seems like removing the extra '|$' in the look ahead did the trick.
Look ahead also matches end of string apparently - I'm assuming
r'.*?(?=^[^\S\n]*this)'
Upvotes: 0