Regex to match the first occurance starting from the end of the string

Viewed 35

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

1 Answers

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 word start and everything after that till end in capture group #1. \b are necessary to avoid it matching restart or starting words.

PS: Since .* is greedy in nature before start it consume longest possible string before matching last start

Related