Learn practical skills, build real-world projects, and advance your career

DS200 Assignment 1 of Aneesh Tickoo

I have created a class "PatternNum" which compiles the regex pattern according to our needs. In that class there is a method named "matchNum" which is finding patterns in mobile numbers and printing them in our required format.

import re
class PatternNum():
    count=0
    pattern = re.compile(r'^(\+\d{1,3}[ ])?[(]?(\d{3})[)-.]?[ ]?(\d{3})[-. ]?(\d{4})$',re.M)
    def __init__(self,data):
        self.data=data
    def matchNum(self):
        matches = PatternNum.pattern.finditer(self.data)
        for match in matches:
            PatternNum.count+=1
            print(f'Number {PatternNum.count} is :', match.group(2)+"-"+match.group(3)+"-"+match.group(4))
data='''
(123) 456-7890
+1 123 456-7890
123.456.7890
1234567890
Demo Text
++23 333 221 2222
456.324.2333
+89 343 323 3333
123-456-7890

'''
data_string=PatternNum(data)
data_string.matchNum()
Number 1 is : 123-456-7890 Number 2 is : 123-456-7890 Number 3 is : 123-456-7890 Number 4 is : 123-456-7890 Number 5 is : 456-324-2333 Number 6 is : 343-323-3333 Number 7 is : 123-456-7890

In the following version instead of making an iterable for the whole data string the numbers get passed indiviually as objects of the class. They are checked and formatted to our required format.