regex - Python regular expression findall * -


i not able understand following code behavior.

>>> import re >>> text = 'been' >>> r = re.compile(r'b(e)*') >>> r.search(text).group() 'bee' #makes sense >>> r.findall(text) ['e'] #makes no sense 

i read existing question , answers capturing groups , all. still confused. please explain me.

the answer simplified in regex howto

as can read here, group returns string matched regular expression.

group() returns substring matched re.

but action of findall justified in documentation

if 1 or more groups present in pattern, return list of groups; list of tuples if pattern has more 1 group

so getting matched part of capture group.

some experiments include :

>>> r = re.compile(r'(b)(e)*') >>> r.findall(text) [('b', 'e')] 

here regex has 2 capturing groups, returned values list of matched groups (in tuples)


Comments

Popular posts from this blog

angularjs - ADAL JS Angular- WebAPI add a new role claim to the token -

php - CakePHP HttpSockets send array of paramms -

node.js - Using Node without global install -