According to an online tutorial which I am reading, it is stated that:
Optional Matching with the Question Mark
"Sometimes there is a pattern that you want to match only optionally. That is, the regex should find a match whether or not that bit of text is there. The ? character flags the group that precedes it as an optional part of the pattern. For example, enter the following into the interactive shell:"
>>> batRegex = re.compile(r'Bat(wo)?man')
>>> mo1 = batRegex.search('The Adventures of Batman')
>>> mo1.group()
'Batman'
My Problem:
I am trying to find matching phone numbers, in the form of either 123-456-7890
(without country code) or (111)-123-456-7890
(with country code).
Here is my regex code for python to return a list of matching phone numbers:
phone_num_regex = re.compile(r'(\(\d{3}\)-)?\d{3}-\d{3}-\d{4}')
phone_num_list = phone_num_regex.findall('800-420-7240 (933)-415-863-9900 415-863-9950')
However, the phone_num_list I obtained is ['', '(933)-', '']
instead of what I wanted which is ['800-420-7240, '(933)-415-863-9900', '415-863-9950']
.
May I know what is wrong with my code? I'm guessing it's something to do with the '?' (optional matches)