I am using the following code to filter out phone numbers from a text file.
import re
pattern = (r'\d*-\d*-\d*')
names = (r'\w*')
with open('records.txt', 'r') as f:
for line in f:
matches = re.findall(pattern, line)
namesMatch = re.findall(names, line)
if matches:
print(matches)
elif namesMatch:
print(namesMatch + ":")`
I am getting the following output:
['John']
['222-333-4447']
['423-444-5678']
['123-455-1223']
['Paul']
['423-444-5778']
I want a similar output but in string format like below:
John:
222-333-4447
423-444-5678
123-455-1223
Paul:
423-444-5778
enter code here['222-333-4447'] ['423-444-5678'] ['123-455-1223'] ['Paul'] ['423-444-5778'] I want a similar output but in string format like below: John: 222-333-4447 423-444-5678 123-455-1223 Paul: 423-444-5778re.findall(r'(\w+):((?:\s*\d+-\d+-\d+)+)', f.read())? Or, something like regex101.com/r/7FaFEk/1?