2

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
11
  • I am getting the following output: ['123-455-1223'] ['222-333-4447'] ['423-444-5678'] ['123-455-1223'] ['221-353-4747'] ['423-444-5778'] I want a similar output but in string format like below: 123-455-1223 222-333-4447 423-444-5678 123-455-1223 221-353-4747 423-444-5778 Commented Jun 20, 2022 at 19:02
  • 1
    it sounds like you have more details to add to this question ... please add the additional comments to the question as code, include both a given input and the expected output for that input Commented Jun 20, 2022 at 19:25
  • @JoranBeasley I have edited the code. I am getting the following output: ['John']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-5778 Commented Jun 20, 2022 at 19:33
  • please provide a snippet of sample input and then we can probably help ... and make sure our example input actually looks like your input because the answer you get will look like that @Nithin Commented Jun 20, 2022 at 19:34
  • 1
    Maybe re.findall(r'(\w+):((?:\s*\d+-\d+-\d+)+)', f.read())? Or, something like regex101.com/r/7FaFEk/1? Commented Jun 20, 2022 at 19:47

1 Answer 1

1

You can use

import re
text = """John................. .............222-333-4447....... .. 423-444-5678 .... .................123-455-1223............... ........Paul... ......423-444-5778....................................... – 
Nithin"""
pattern=r"(\w+)((?:[\s:.]*\d+[-—–]\d+[-—–]\d+)+)"
print( [(x, " ".join(y.replace('.','').split())) for x,y in re.findall(pattern, text)] )

Output:

[('John', '222-333-4447 423-444-5678 123-455-1223'), ('Paul', '423-444-5778')]

See the Python demo.

Details:

  • (\w+) - Group 1: one or more word chars
  • ((?:[\s:.]*\d+[-—–]\d+[-—–]\d+)+) - Group 1: one or more occurrences of the following pattern sequence
    • [\s:.]* - zero or more whitespaces, colons and dots
    • \d+ - one or more digits
    • [-—–] - a hyphen or dash
    • \d+[-—–]\d+ - one or more digits, a dash, one or more digits.
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.