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']
-Why findall output matches 'blah' three times, when its specified {2} times only in the pattern?
If I change to {4}, then findall matches:
asd = re.compile(r"(blah){4}")
findall output: ['blah']
-How is {m} treated with re.search and re.findall ?
Thanks a lot.
If you want to catch the
(blah){2}
(the 2blah
you have there) you should wrap it:Exactly the same goes with the
{4}
you have there. Theregex
will find it, but will not catch it. if you want to catch it you should wrap it.