[英]How to use {} in regex pattern with findall + Python
I'm creating a regex as below: 我正在创建一个正则表达式,如下所示:
import re
asd = re.compile(r"(blah){2}")
mo = asd.search("blahblahblahblahblahblah ll2l 21HeHeHeHeHeHe lllo")
mo1 = asd.findall("blahblahblahblahblahblah")
print(mo.group())
print("findall output: ", mo1)
This returns output blahblah findall output: ['blah', 'blah', 'blah'] 这将返回输出blahbla findall输出:['blah','blah','blah']
-Why findall output matches 'blah' three times, when its specified {2} times only in the pattern? -为什么当findall输出仅在模式中指定{2}次时,findall输出与“ blah”匹配3次?
If I change to {4}, then findall matches: 如果我更改为{4},则findall匹配:
asd = re.compile(r"(blah){4}")
findall output: ['blah']
-How is {m} treated with re.search and re.findall ? -如何使用re.search和re.findall处理{m}?
Thanks a lot. 非常感谢。
If you want to catch the (blah){2}
(the 2 blah
you have there) you should wrap it: 如果您想抓住
(blah){2}
(那里有2个blah
),则应该将其包装起来:
asd = re.compile(r"((?:blah){2})")
Note that I made sure not to catch the inside
blah
(using?:
)请注意,我确保不要抓住里面的东西(使用
?:
blah
>>>asd = re.compile(r"((?:blah){2})")
>>>mo = asd.search("blahblahblahblahblahblah ll2l 21HeHeHeHeHeHe lllo")
>>>mo1 = asd.findall("blahblahblahblahblahblah")
>>>print(mo.group())
blahblah
>>>print("findall output: ", mo1)
findall output: ['blahblah', 'blahblah', 'blahblah']
Exactly the same goes with the {4}
you have there. 那里的
{4}
完全一样。 The regex
will find it, but will not catch it. regex
可以找到它,但不会捕获它。 if you want to catch it you should wrap it. 如果您想抓住它,则应该将其包裹起来。
(blah){2}
captures and exhausts the string blahblah
but only returns the last blah
in blahblah
. (blah){2}
捕获和排气字符串blahblah
但仅返回最后 blah
在blahblah
。 Since you have three blahblah
s in your string, it will output ['blah', 'blah', 'blah']
由于您的字符串中包含三个
blahblah
,因此它将输出['blah', 'blah', 'blah']
(blah){4}
can only match once so it gives you ['blah']
(blah){4}
只能匹配一次,因此可以给您['blah']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.