2

Could anyone figure out why these 2 results are totally different:

import re

a = re.findall(r'\w+(?=,|\.)',"They were three: Felix, Victor, and Carlos.") ; print(a)
a = re.findall(r'\w+(?=(,|\.))',"They were three: Felix, Victor, and Carlos.") ; print(a)

1st return : ['Felix', 'Victor', 'Carlos']

2nd return: [',', ',', '.']

Thanks, Ascanio

Tushar
  • 85,780
  • 21
  • 159
  • 179
Ascanio
  • 69
  • 1
  • 6
  • 1
    `re.findall` - that is why. It returns the captured texts if you specify capturing groups. Capturing works even inside a look-around. Use `r'\w+(?=(?:,|\.))'` to obtain the same results in both cases. – Wiktor Stribiżew Nov 06 '15 at 09:06
  • And the link to the description: https://docs.python.org/2/library/re.html#re.findall – Mariano Nov 06 '15 at 09:09
  • Thanks stribizhev! This is really helpful! – Ascanio Nov 06 '15 at 09:16
  • @stribizhev: could you please look into another question? http://stackoverflow.com/questions/33580468/python-regex-look-ahead-positive-negative – Ascanio Nov 07 '15 at 08:21

0 Answers0