Preliminaries
For this lab you will be working with regular expressions in Python. Various functions for working with regular expressions are available in the re module. Fortunately, Python makes it pretty easy to check if a string matches a particular pattern.
At the top of the file we must import the re module: import re
Then we can use the search() function to test whether a string matches a pattern. In the example below, the regular expression has been saved in a string called pattern for convenience:
phone = "123-456-7890" pattern = r"ˆ\d{3}-\d{3}-\d{4}$" if re.search(pattern, phone):
print("The string matches the pattern.") else:
print("The string does not match the pattern.")
The r that precedes the pattern string is not a typo. Rather, the r indicates that the string is a “raw” string. In a raw string, as opposed to a “normal” string, any backslash character is interpreted as simply a backslash, as opposed to defining an escape sequence like \n or \t. Make sure you use raw strings in your Python code when defining regular expressions.
The ˆ and $ at the respective beginning and end of the regular expression indicate that the entire string must match the regular expression, and not just part of the string. Make sure you include these symbols in your regular expressions too!
You can play around with regular expressions on this web=based platform: regex101.com.Part I: Phone Number Checker
PART 1
Write a Python function phone number() that takes one parameter, which is a list of strings, each being a potential phone number. The function examines each string in the list and returns a list of indexes of those strings which meet the following description/requirement:
A valid string starts with (i) an optional open parenthesis, "("; followed by (ii) three digits (the first one being non-zero); then followed by (iii) an optional close parenthesis, ")" (the close parenthesis will only be present if there is a corresponding open parenthesis and vice versa); then followed by (iv) an optional hyphen, "-"; followed by (v) three more digits; followed by (vi) an optional hyphen,"-" again; and finally ends with (vii) four more digits.
Few valid examples of this format are: "(631)111-2211",
"631111-2211", "631-111-2211"etc.
Fewinvalidexamplesare:"(631111-2211","631
111-2211","631)-111-2211"etc.
Note: A validly formatted string may not contain any spaces.Examples:
Function Arguments |
Return Value |
[’(091)-111-1234’, ’6311112222’, ’(631)-111-2222’, ’(631) 111-2222’] |
[1, 2] |
[’091-111-1234’, ’631 111 2222’, ’(631)1112222’, ’0911111234’] |
[2] |
[’631-1111-234’, ’631--111-2222’, ’(6311112222’, ’631)1111234’] |
[] |
[’(631)-8675309’, ’6311112222’, ’(631)111-2222’, ’(631)1111234’] |
import re def phonenumber(numbers): valid = [] for i in range(0, len(numbers)): # If current number in list is valid # Append its index to valid list if validateNumber(numbers[i]): valid.append(i) # Return valid list of indexes return valid def validateNumber(num): # ^ matches start of string # \( matches opening brace # \[1-9]{3} matches [1-9] 3 digits # \) matches closing brace # \d{3} matches 3 digits # -? matches -, 0 or 1 times # \d{4} matches 4 digits # $ matches end of string pattern1 = r"^[1-9]{3}-?\d{3}-?\d{4}$" pattern2 = r"^\([1-9]{3}\)-?\d{3}-?\d{4}$" # If number matches any pattern of the two, means its valid # return true if re.search(pattern1, num) or re.search(pattern2, num): return True else: return False if __name__ == '__main__': print(phonenumber(['(091)-111-1234', '6311112222', '(631)-111-2222', '(631) 111-2222'])) print(phonenumber(['091-111-1234', '631 111 2222', '(631)1112222', '0911111234'])) print(phonenumber(['631-1111-234', '631--111-2222', '(6311112222', '631)1111234'])) print(phonenumber(['(631)-8675309', '6311112222', '(631)111-2222', '(631)1111234']))
SCREENSHOT
OUTPUT
Preliminaries For this lab you will be working with regular expressions in Python. Various functions for...
IN PYTHON. We have seen examples of using the python re library. In this practice, you need to write python code and complete following tasks. (You can decide whether you want to define functions and how to organize your code. It is for your own practice.) 1). Define 10 string variables that follows the requirement given an integer, a float, a double, a float end with letter f (4.321f), Capital Letters( followed by small case letters, followed by digits, Exactly...
Use the Python “re” module to do the following: 1. Write a regular expression pattern that matches string “March 1, 2019, Mar 1, 2019, March First, 2019, March First, 19”. No credit for not using special characters: “*,+,?, | and etc” to do the matching. 2. Write a regular expression pattern that matches strings representing trains. A single letter stands for each kind of car in a train: Engine, Caboose, Boxcar, Passenger car, and Dining car. There are four rules...
please Answer the following regular expressions questions(also do number 9) Q4 Choose the pattern that finds all filenames in which the first letters of the filename are astA, followed by a digit, followed by the file extension .txt. 1) astA[[:digit:]]\.txt 2) astA[[0-9]].txt 3) astA.\.txt 4) astA[[:digit:]].txt Q5 What's the difference between [0-z]+ and \w+ ? 1) The first one accepts 0 and z and the other doesn't. 2) The first one doesn't allow for uppercase letters. 3) The first one...
Your mission in this programming assignment is to create a Python program that will take an input file, determine if the contents of the file contain email addresses and/or phone numbers, create an output file with any found email addresses and phone numbers, and then create an archive with the output file as its contents. Tasks Your program is to accomplish the following: ‐ Welcome the user to the program ‐ Prompt for and get an input filename, a .txt...