yasu.neko yasu.neko - 11 days ago 6
Python Question

finding repeated characters using re.match

I'm trying to figure out why the following code prints "2" instead of "1"

import re

line = "9111222"
m = re.match( r'.*(\w)\1+', line)
print m.group(1)


I understand that re.match attempts to match at the beginning of the string, but I assumed it would then see the "111" and print "1"

Answer

The * means greedy, add a ? behind to make it non greedy if you were interested on the 111

import re

line = "9111222"
#                 ^
m = re.match( r'.*?(\w)\1+', line)
print(m.group(1))
# '1'