简体   繁体   中英

Issue Validating more than one pattern of regex in Python

I am having issues with regex and re.search method. The expected result is validation for both phone numbers and emails. I can validate for phone numbers or emails, but when I want to do both, I only get phone numbers being recognized.

I tried separating them into different functions and only when commenting out phone number function would email addressed be recognized.

#!/usr/bin/env python3
'''regex program for retrieving phone numbers & emails from a list,
this will use 2 regex patterns. One for phone numbers & one for emails'''
import re
import sys


if len(sys.argv) < 2:
    sys.exit("Usage: run as follows: extractor.py [phone number or email address]")

string = ' '.join(sys.argv[1:])

PHONE_NUMBER = re.compile(r"""(
    (\(\d{3}\))?|(\d{3})    # area code check
    (\d{3})                 # First 3 digits
    (\s|-|\.)               # Hyphen
    (\d{4})                 # Last 4 digits
    )""", re.VERBOSE)       # Verbose method
# \d{3}\)\d{3}-\d{4}'))

EMAIL = re.compile(r"""(
    [a-zA-Z0-9._%&+-]+      # email userid
    @                       # @ sign for email domain
    [a-zA-Z0-9.-]+          # domain
    (\.[a-zA-Z0-9]{2,3})    # dot.com/etc, a range of 2-3
    )""", re.VERBOSE)


def phone_num_email():
    '''phone numbers or email addresses'''
    if(re.search(PHONE_NUMBER, string)):
        print("Found a match for a phone number")
    elif re.search(EMAIL, string):
        print("Found a match for email address")
    else:
        print("No matches found for a phone number or email address")


# TODO:
# recognized email, still has blank acceptance of phone numbers


if __name__ == '__main__':
    phone_num_email()

You are utilising your if statements incorrectly to achieve the desired result.

if(re.search(PHONE_NUMBER, string)):
        print("Found a match for a phone number")
elif re.search(EMAIL, string):
        print("Found a match for email address")

You're saying you only want to search for an email, if a phone number is not found. This is due to else if . In the case of searching for both, you do not want to use an else statement as it will ignore the email if a phone number match is found.

Change it to:

if re.search(PHONE_NUMBER, string):
        print("Found a match for a phone number")
if re.search(EMAIL, string):
        print("Found a match for email address")
else:
        print("No matches found for a phone number or email address")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM