Return Python 中通配符匹配的内容

Return the content of a Wildcard match in Python

是否可以 return 在 Python 中以正则表达式模式匹配通配符(如 .*)的内容?

例如,匹配如下:

re.search('stack.*flow','Whosebug') 

会 return 字符串 'over'。

是的,您可以捕获您的结果。为此,只需使用 ()

matchobj = re.search('stack(.*)flow','Whosebug') 
print(matchobj.group(1)) # => over

使用捕获组:

>>> import re
>>> re.search('stack(.*)flow', 'Whosebug').group(1)
'over'