4

As I have read (dog|cat)food will look for dog food and cat food but I am unable to reproduce it with my case.

>>> for m in re.findall('RA[a-zA-Z0-9]*',"RAJA45909"):
    print(m)


RAJA45909
>>> for m in re.findall('(ra|RA)[a-zA-Z0-9]*',"RAJA45909"):
    print(m)


RA
>>> 

Could somebody help me to understand this.

Bakuriu
  • 92,520
  • 20
  • 182
  • 218
rɑːdʒɑ
  • 4,925
  • 13
  • 41
  • 77

2 Answers2

4

You should use re.finditer instead of re.findall and then print the whole matching group:

>>> for m in re.finditer('(ra|RA)[a-zA-Z0-9]*',"RAJA45909"):
...     print(m.group())
... 
RAJA45909

The documentation of findall says:

If one or more groups are present in the pattern, return a list of groups; this will be a list of tuples if the pattern has more than one group.

Your regex has only one group and thus the result is a list of texts matched by that single group. If we add an other group you see:

>>> for m in re.findall('(ra|RA)([a-zA-Z0-9]*)',"RAJA45909"):
...     print(m)
... 
('RA', 'JA45909')

So findall when used with groups matches the whole regex but only returns the portions matched by the groups. While finditer always returns a complete match object.

Bakuriu
  • 92,520
  • 20
  • 182
  • 218
-1

You can use this

print(re.findall('((?:ra|RA)[a-zA-Z0-9]*)',"RAJA45909"))

Ideone Demo

rock321987
  • 10,620
  • 1
  • 26
  • 38