0

Can you look at the expression and the output pair below and tell me how it differs?

1.

numregex=re.compile(r'\d{3}')
numregex.findall('the num is 123431532524542524')

output>>['123', '431', '532', '524', '542', '524']
numregex=re.compile(r'(\d){3}')
numregex.findall('the num is 1232143892432')

output>>['3', '4', '9', '3']
numregex=re.compile(r'(\d\d){3}')
numregex.findall('the num is 123431532524542524')

output>>['31', '24', '24']
Barmar
  • 741,623
  • 53
  • 500
  • 612
The Cook
  • 21
  • 3
  • When you quantify a capture group, it only captures the first instance. – Barmar Aug 13 '20 at 19:17
  • @Barmar suppose for the examples i gave i don't have to add a capturing group and i can write it as \d{3} to find the desired result. But take this is an example -2-2-2 when i write the regular expression as re.compile(r'(-/d){3}') and run the findall method it only captures one instance and it is the last of (-\d-\d-\d). So could you tell me a way to write the regex to find these instances without having to manually type -\d-\d-\d – The Cook Aug 14 '20 at 08:26
  • Put the capturing group around the quantified group: `r'((:?-\d){3})'` – Barmar Aug 14 '20 at 14:26

0 Answers0