Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

regex

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

like image 687
Blob Avatar asked Oct 12 '25 07:10

Blob


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

like image 110
anubhava Avatar answered Oct 14 '25 21:10

anubhava