0

I am experimenting with for regex lazy matches. when I tried this:

>>> text='<em>Hello <>World</em>'
>>> pattern3=re.compile('<.*?>')
>>> for mat in re.findall(pattern3,text):
...     print mat
... 
<em>
<>
</em>

It works as expected, and gives the three matches that are possible but when I tried this:

>>> text="1011"
>>> pattern1=re.compile('1.*?1')
>>> for mat in re.findall(pattern1,text):
...     print mat
... 
101

I get only one output, but I should get 101 and 11, why 11 is not being matched. my regex '1.*?1' looks for 1 followed by zero or more of anything and a 1 again.

eagertoLearn
  • 9,772
  • 23
  • 80
  • 122
  • The short answer is because of overlapping matches. The second `1` is matched in the first match and cannot 'participate' in another match again. Try to look around for overlapping matches. I'm sure it has been asked before. For example [this](http://stackoverflow.com/q/15799332/1578604) maybe? – Jerry Feb 20 '14 at 20:07

1 Answers1

0

Did you try this for overlapping match?

pattern1=re.compile('(?=(1.*?1))')
Sabuj Hassan
  • 38,281
  • 14
  • 75
  • 85