Question

In: Computer Science

The phone numbers collected from questionnaire is a mess. Design a regular expression to filter out...

  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” 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]

Solutions

Expert Solution

The following is the code for above question

import re
fin=open("/content/Q1.txt","r") #open file in read mode
data=fin.readlines()
fout=open("cleaned.txt","w")  #open file in write mode
for item in data:
  match=re.search("^[+][0-9]{2}[-][0-9]{1}[-][0-9]{4}[-][0-9]{4}$",item.strip()) #Accepts only numbers of type "+00-0-0000-0000"
  if match:  #if match then accept the number and write it into cleaned.txt file
    fout.write(item)
fin.close()  #close the Q1.txt file
fout.close()  #close the cleaned.txt file

I am also attaching the screenshots for the above code and output as well.

Input:

+61-0-1234-3333
+91-3-5656-4232
+1-5-4564-2222
+51-5002-3323-8787
43-0-4444-4343
+78-0-343-3422
+01-9-2662-276
+20-6-3535-4001

Output:


#Please dont forget to upvote if you find the solution helpful. Feel free to ask doubts if any, in the comments section. Thank you.


Related Solutions

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.
Design a regular expression to filter out those numbers that are stored in the standard format  “+00-0-0000-0000”....
Design a regular expression to filter out those numbers that are stored in the standard format  “+00-0-0000-0000”. Examples of valid results are: +61-3-9214-4980 and +61-3-9285-7706
linux: Regular expressions file name: studentsyslog.txt Use a regular expression and grep or egrep to filter...
linux: Regular expressions file name: studentsyslog.txt Use a regular expression and grep or egrep to filter the file so the output displays only the items requested. Put your full command in the space provided 1.Show only the lines that end with an ellipses (3 dots) :
linux: regular expressions file name: studentsyslog.txt Use a regular expression and grep or egrep to filter...
linux: regular expressions file name: studentsyslog.txt Use a regular expression and grep or egrep to filter the file so the output displays only the items requested. Put your full command in the space provided. 1. Display only the lines that were written to the file between the times of 12:55 and 12:59 (inclusive). This is tricky. Don’t think of these times as numbers, think of these times as a series of characters (a 1 followed-by a 2 followed-by a colon,...
linux: regular expressions file name: studentsyslog.txt Use a regular expression and grep or egrep to filter...
linux: regular expressions file name: studentsyslog.txt Use a regular expression and grep or egrep to filter the file so the output displays only the items requested. Put your full command in the space provided. 1. Display only the lines that were written to the file between the times of 12:55 and 12:59 (inclusive). This is tricky. Don’t think of these times as numbers, think of these times as a series of characters (a 1 followed-by a 2 followed-by a colon,...
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 #...
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
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)...
Design a low rate trickling filter for secondary treatment of sewage generated from 10,000 persons with...
Design a low rate trickling filter for secondary treatment of sewage generated from 10,000 persons with rate of water supply 170 liters per capita per day. The BOD5 after primary treatment is 110 mg/L and BOD5 of final effluent should be 20 mg/L. Consider the following equation, where C = 5.358. (St/So)=(1)/(1+C((D^0.67)/(QL^0.5)) Now, design a high rate trickling filter for the data given above except effluent BOD5 is 40 mg/l since polishing treatment is provided after high rate trickling filter....
Data is collected from a completed randomized design for comparing two treatments A and B: A:...
Data is collected from a completed randomized design for comparing two treatments A and B: A: 25, 24, 22 B: 27, 30, 29 The experimenter is interested in finding out if treatment B is better than treatment A. Find the p-values using both the randomization test and the t-test and then make a conclusion (alpha = 0.05)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT