-1
import re
groupRegex = re.compile(r'(\d\d\d).(\d\d\d)-(\d\d\d\d)')                            
print(groupRegex.findall('Cell: 514.552-2345 Work: 512-323-5543'))

[('514', '552', '2345'), ('512', '323', '5543')]

Why doesn't it return [('514', '552', '2345')] and not the "Work" phone number? The period in this instance is acting like a hyphen. Please explain in the most basic way, thanks.

Flux87
  • 1

2 Answers2

0

The regex compiler will compile the . as a regex operator and not the "dot" as a character. You must escape it to get the correct output.

import re
groupRegex = re.compile(r'(\d\d\d)\.(\d\d\d)-(\d\d\d\d)')                            
print(groupRegex.findall('Cell: 514.552-2345 Work: 512-323-5543'))

[('514', '552', '2345'), ('512', '323', '5543')]
Chintan Rajvir
  • 689
  • 6
  • 20
-1

"." in regex matches any character. If you want to match a literal . you need to use \. to escape it

Ben K
  • 344
  • 2
  • 5