python program that could search the text in a file for phone numbers and email addresses

Develop a python program that could search the text in a file for phone numbers (+919876543210) and email addresses ([email protected])

Program:-

import re
phone_regex = re.compile(r'\+\d{12}')
email_regex = re.compile(r'[A-Za-z0-9._]+@[A-Za-z0-9]+\.[A-Z|a-z]{2,}')
with open('example.txt', 'r') as f:
    for line in f:
        matches = phone_regex.findall(line)
        for match in matches:
            print(match)
        matches = email_regex.findall(line)
        for match in matches:
            print(match)

Output:-

+911234567890
+820987456321
+919876543210
[email protected]

Leave a Reply

Your email address will not be published. Required fields are marked *