yasu.neko yasu.neko - 9 months ago 81
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 Source

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)
# '1'