Understanding '?' in regular expressions

krishna.k.kishor3 at gmail.com krishna.k.kishor3 at gmail.com
Fri Nov 16 08:28:21 CET 2012


Can someone explain the below behavior please?

>>> re1 = re.compile(r'(?:((?:1000|1010|1020))[ ]*?[\,]?[ ]*?){1,3}')
>>> re.findall(re_obj,'1000,1020,1000')
['1000']
>>> re.findall(re_obj,'1000,1020, 1000')
['1020', '1000']

However when I use "[\,]??" instead of "[\,]?" as below, I see a different result
>>> re2 = re.compile(r'(?:((?:1000|1010|1020))[ ]*?[\,]??[ ]*?){1,3}')
>>> re.findall(re_obj,'1000,1020,1000')
['1000', '1020', '1000']

I am not able to understand what's causing the difference of behavior here, I am assuming it's not 'greediness' if "?"

Thank you,
Kishor


More information about the Python-list mailing list