<p>我有个问题。假设有这样的模式:</p>
<pre><code>>>> import re
>>> pt ='^a{1,2}$'
>>> re.search(pt, 'aa') # looks ok
<_sre.SRE_Match object at 0x020B2288>
>>> re.search(pt, 'aaa') # ok too
>>>
</code></pre>
<p>现在,让我们尝试寻找与另一个模式匹配的:</p>
<pre><code>>>> 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>
</code></pre>
<p>(1)(2)(3)在我看来,它应该匹配以一个或两个“a”或一个“x”开头的字符串,或者两个字母的组合,以及这些字母之间的结尾,而不是别的。或者我不明白?应该是这样吗?就像使用“|”时,它忽略了{}中的限制?
有人能给我解释一下吗?你知道吗</p>