import re
phoneNumRegex = re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)')
mo = phoneNumRegex.search('Phone number: 415-555-4242')
print(mo.group(1))
print(mo.group(2))
print(mo.group(0))
print(mo.groups())

Output:

415
555-4242
415-555-4242
('415', '555-4242')

Discover more from Tips and Hints for Aerospace Engineers

Subscribe now to keep reading and get access to the full archive.

Continue reading