Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
python正则表达式findall无法正确匹配_Python_Regex - Fatal编程技术网

python正则表达式findall无法正确匹配

python正则表达式findall无法正确匹配,python,regex,Python,Regex,我想匹配“[M]+”和“[M]2+” 但结果是 re.findall("\[M(\+\d)?\](\d)?\+", u'[2][M]+[2][M]2+') 您已在regex模式中捕获组,该模式将返回组;如果需要返回匹配项,可以使用?:将捕获的组转换为未捕获的组: [('', ''), ('', '2')] import re re.findall("\[M(?:\+\d)?\](?:\d)?\+", u'[2][M]+[2][M]2+') # ^^

我想匹配“[M]+”和“[M]2+”
但结果是

re.findall("\[M(\+\d)?\](\d)?\+", u'[2][M]+[2][M]2+')

您已在regex模式中捕获组,该模式将返回组;如果需要返回匹配项,可以使用
?:
将捕获的组转换为未捕获的组:

[('', ''), ('', '2')]
import re
re.findall("\[M(?:\+\d)?\](?:\d)?\+", u'[2][M]+[2][M]2+')
#               ^^         ^^
# ['[M]+', '[M]2+']