2
import re

line = 'Here is my probblem, brother!'

t = re.findall('..b', line)

print(t)

This prints:

['rob', ', b']

But it should find 'obb' in 'probblem'. Why?

orhanodabasi
  • 962
  • 7
  • 11

1 Answers1

5

Because . will match one character, and in this case you have 'ro' and ', ' which are followed by one b. And with regards to this point that finall() function doesn't match overlapped patterns if you want to match such patterns, you can use a positive look ahead and put your pattern in a capture group :

>>> t = re.findall('(?=(..b))', line)
>>> t
['rob', 'obb', ', b']
Mazdak
  • 105,000
  • 18
  • 159
  • 188