I have a question. Lets say there are such patterns:
>>> import re
>>> pt ='^a{1,2}$'
>>> re.search(pt, 'aa') # looks ok
<_sre.SRE_Match object at 0x020B2288>
>>> re.search(pt, 'aaa') # ok too
>>>
Now lets try to look for match with another pattern:
>>> pt = '^a{1,2}|x$'
>>> re.search(pt, 'a') # this one looks ok
<_sre.SRE_Match object at 0x020B25D0>
>>> re.search(pt, 'aaax') # (1) Now this one?
<_sre.SRE_Match object at 0x020B2288>
>>> re.search(pt, 'aaaaaax') # (2) and this one?
<_sre.SRE_Match object at 0x020B25D0>
>>> re.search(pt, 'aaa') # (3) and this one?
<_sre.SRE_Match object at 0x020B25D0>
(1)(2)(3) To me it looks like it should match string that starts with one or two ‘a’ or one ‘x’ or both combinations and ends between these letters, but nothing else. Or I don’t get it something? Is it should be like that? Like when you use ‘|’, it ignores what limit is put inside {}?
Can someone explain me this?
The
$is affected by the grouping. Your regex is interpreted as(^a{1,2})|(x$), which matches either “one or two as at the beginning of the string” OR “an x at the end of the string”. If you want to have the|apply only to the as and xs, you need to group them:Or, if you don’t want to capture the group, use a noncapturing group:
Edit: I’m not sure I understand what you’re trying to match, but perhaps try: