How do you match the first occurance of start, starting from the end of the string? I have tried it with a negative lookahead but instead I get start\nfoo\nmoo\nstart\nfoo\ndoo as match
import re
pattern='(start[\s\S]*$)(?=$)'
string='start\nfoo\nmoo\nstart\nfoo\ndoo'
re.search(pattern, string)
expected match: start\nfoo\ndoo
CodePudding user response:
You can use this code:
string='start\nfoo\nmoo\nstart\nfoo\ndoo'
print (re.findall(r'(?s).*(\bstart\b.*)', string))
##> ['start\nfoo\ndoo']
RegEx Breakup:
(?s): Enable single line or DOTALL mode to make dot match line break as well.*: Match longest possible match including line breaks(\bstart\b.*): Match wordstartand everything after that till end in capture group #1.\bare necessary to avoid it matchingrestartorstartingwords.
PS: Since .* is greedy in nature before start it consume longest possible string before matching last start
