Question

In: Computer Science

Q1 Write a python regular expression to match a certain pattern of phone number.             ###...

Q1 Write a python regular expression to match a certain pattern of phone number.

            ### Suppose we want to recognize phone numbers with or without hyphens. The

### regular expression you give should work for any number of groups of any (non-

### empty) size, separated by 1 hyphen. Each group is [0-9]+.

### Hint: Accept "5" but not "-6"

### FSM for TELEPHONE NUMBER IS:

# state:1 --[0-9]--> state:2

# state:2 --[0-9]--> state:4

# state:2 --[\-]---> state:3

# state:3 --[0-9]--> state:4

# state:4 --[\-]---> state:3

# state:4 --[0-9]--> state:4

import re

regexp = ###Your Regular Expression Here.

### regexp matches:

print(re.findall(regexp,"123-4567") == ["123-4567"])

#>>> True

print(re.findall(regexp,"1234567") == ["1234567"])

#>>> True

print(re.findall(regexp,"08-78-88-88-88") == ["08-78-88-88-88"])

#>>> True

print(re.findall(regexp,"0878888888") == ["0878888888"])

#>>> True

### regexp does not match:

print(re.findall(regexp,"-6") != ["-6"])

#>>> True

Solutions

Expert Solution

RAW CODE

import re

regexp = '[^-][0-9-]+' ###Your Regular Expression Here.

### regexp matches:
print(re.findall(regexp,"123-4567") == ["123-4567"])
#>>> True

print(re.findall(regexp,"1234567") == ["1234567"])
#>>> True

print(re.findall(regexp,"08-78-88-88-88") == ["08-78-88-88-88"])
#>>> True

print(re.findall(regexp,"0878888888") == ["0878888888"])
#>>> True

### regexp does not match:
print(re.findall(regexp,"-6") != ["-6"])
#>>> True

SCREENSHOTS (CODE AND OUTPUT)

NOTE:- Here ' [^-] ' represents that should not start with ' - '. ' [0-9-]+ ' represents combination of numbers from 0-9 along with ' - ' anywhere in between and plus represents all the combinations except null value.

##### FOR ANY QUERY, KINDLY GET BACK, THANKYOU. #####


Related Solutions

Write the correct regular expression pattern for a phone number field with the format XXX-XXX-XXXX
Write the correct regular expression pattern for a phone number field with the format XXX-XXX-XXXX
Q1. 1. The phone numbers collected from questionnaire is a mess. Design a regular expression to...
Q1. 1. The phone numbers collected from questionnaire is a mess. Design a regular expression to filter out those numbers that are stored in the standard format "+00-0-0000-0000" from the file called "Q1.txt". Q1.txt +61392144979 +61 39214 4778 +61-3-9214-4980 +66(2)51574430 +61-3-9285-7706 Note: Only +61-3-9214-4980 and +61-3-9285-7706 are the valid results.
Question d) Write the regular expression statement that will match a string that starts with the...
Question d) Write the regular expression statement that will match a string that starts with the word "Warning" following by one space and then a number of any number of digits. Question e) Write the regular expression statement that will match a string that ends with the phrase "End of Report. Question f) Write the regular expression statement that will match a string that starts with the phrase "Begin Log:" then contains a collection of characters (other than newline) followed...
Create and test a python regular expression that matches a street address consisting of a number...
Create and test a python regular expression that matches a street address consisting of a number with one or more digits followed by two words of one or more characters each. The tokens should be separated by one space each, as in 123 Main Street.
Pattern matching using python3 re module Write a regular expression that will find out all the...
Pattern matching using python3 re module Write a regular expression that will find out all the words that ends with 4 consecutive vowels at the end. Example: import re f=open("/usr/share/dict/american-english",'r') pattern='a[a-z]*d$' words=f.readlines() matchlist=[word for word in words if re.match(pattern,word)] =============================== Generate random string follow a specific pattern You will take a username and ask user for the password. User has to enter a strong password in order to create his or her account. The criteria for strong password is, a)...
1. What is a regular expression? Write a regular expression that will detect “College” and “collegE”....
1. What is a regular expression? Write a regular expression that will detect “College” and “collegE”. 2. What is degree centrality? Create a graph of 4 vertices and compute the degree centrality of the vertices. 3. Compute internal and external community densities for a graph containing 6 nodes. You can create any graph of 6 nodes with at least 4 edges.
Write Python class that takes a string and returns with a valid phone number. Number format...
Write Python class that takes a string and returns with a valid phone number. Number format is ten-digit numbers consisting of a three-digit area code and a seven-digit number. Clean up different telephone numbers by removing punctuation, and removing incorrect format and the country code (1). You should throw a ValueError with a string if there are too many or too few digits, or the wrong digits. For example, the strings: +1 (617) 111-0000, 617-111-0000, 1 617 111 0000, 617.111.0000...
Write a regular expression, using the regular expression syntax used by lex, that matches any finite...
Write a regular expression, using the regular expression syntax used by lex, that matches any finite decimal representation of a nonnegative real number.
Use Python Write a function that takes a mobile phone number as a string and returns...
Use Python Write a function that takes a mobile phone number as a string and returns a Boolean value to indicate if it is a valid number or not according to the following rules of a provider: * all numbers must be 9 or 10 digits in length; * all numbers must contain at least 4 different digits; * the sum of all the digits must be equal to the last two digits of the number. For example '045502226' is...
The phone numbers collected from questionnaire is a mess. Design a regular expression to filter out...
The phone numbers collected from questionnaire is a mess. Design a regular expression to filter out those numbers that are stored in the standard format         “+00-0-0000-0000” from the file called “Q1.txt” and redirect the results to the “cleaned.txt”. Note: Only +61-3-9214-4980 and +61-3-9285-7706 are the valid results.    [10 Marks]
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT