Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

regex of overlapping patterns

I am trying to match all instances of the pattern in the string using Python. However, when the patterns overlap I get only the longest one, while I need both:

import re
st = '''GYMGMTPRLGLESLLEStopAS'''
w = re.findall("M\w*?(?=Stop)",st)
print w

Output:

1. MGMTPRLGLESLLE

Desired output:

1. MGMTPRLGLESLLE
2. MTPRLGLESLLE
like image 494
user3468837 Avatar asked Feb 02 '26 07:02

user3468837


1 Answers

(?=(M\w*?)Stop) Then the overlapp data is in capture group 1.