In: Computer Science
Note: Only +61-3-9214-4980 and +61-3-9285-7706 are the valid results. [10 Marks]
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.