这里有两个问题。您在没有指定组的情况下使用了group(),我可以告诉您,正则表达式的行为与显式括号组和没有括号组的行为感到困惑。您正在观察的这种不带括号的行为只是 Python 提供的一种快捷方式,您需要阅读有关group()的文档才能完全理解它。
>>> import re
>>> string = "baaa"
>>>
>>> # Here you're searching for one or more `a`s until the end of the line.
>>> pattern = re.search(r"a+$", string)
>>> pattern.group()
'aaa'
>>>
>>> # This means the same thing as above, since the presence of the `$`
>>> # cancels out any meaning that the `?` might have.
>>> pattern = re.search(r"a+?$", string)
>>> pattern.group()
'aaa'
>>>
>>> # Here you remove the `$`, so it matches the least amount of `a` it can.
>>> pattern = re.search(r"a+?", string)
>>> pattern.group()
'a'
底线是字符串a+?
匹配一个a
,句点。但是,a+?$
匹配a
's直到行尾。请注意,如果没有明确的分组,您将很难理解?
任何事情。一般来说,无论如何,最好明确说明你用括号分组的内容。让我举一个带有显式组的例子。
>>> # This is close to the example pattern with `a+?$` and therefore `a+$`.
>>> # It matches `a`s until the end of the line. Again the `?` can't do anything.
>>> pattern = re.search(r"(a+?)$", string)
>>> pattern.group(1)
'aaa'
>>>
>>> # In order to get the `?` to work, you need something else in your pattern
>>> # and outside your group that can be matched that will allow the selection
>>> # of `a`s to be lazy. # In this case, the `.*` is greedy and will gobble up
>>> # everything that the lazy `a+?` doesn't want to.
>>> pattern = re.search(r"(a+?).*$", string)
>>> pattern.group(1)
'a'
编辑:删除了与旧版本问题相关的文本。