Suppose I have the following data:
some_string = """
Dave Martin
615-555-7164
173 Main St., Springfield RI 559241122
davemartin101@exampledomain.com
Charles Harris
800-555-5669
969 High St., Atlantis VA 340750509
charlesharris101@exampledomain.com
"""
I used the following to find a pattern:
import re
pattern = re.compile(r'\d\d\d(-|\.)\d\d\d(-|\.)\d\d\d\d')
matches = pattern.finditer(some_string)
Printing the re
object shows:
for match in matches:
print(match)
<re.Match object; span=(21, 33), match='615-555-7164'>
<re.Match object; span=(131, 143), match='800-555-5669'>
I want to extract the span and match fields. I found this link Extract part of a regex match that shows how to use group()
:
nums = []
for match in matches:
nums.append(match.group(0))
I get the following result:
print(nums)
['615-555-7164', '800-555-5669']
Similar to the other StackOverlow thread above, how can I extract the span?
This thread was marked for deletion by someone and then it was deleted. The justification for deletion was that I was seeking advice on software... which I was not. https://i.imgur.com/sbCfekf.png