1. Judgment of foreign mobile phones

 def iphonenum(txt): if len(txt) !=  12: return False for i in range(3): if not txt[i].isdecimal(): return False if txt[3] != '-': return False for j in range(4, 7): if not txt[j].isdecimal(): return False if txt[7] != '-': return False for l in range(8, 12): if not txt[l].isdecimal(): return False return True Print ('please enter mobile phone number: ', end=' ') k =input() Print ('determine whether it is a mobile phone number: ', iphonenum (k))

2. Find the mobile phone number in the text

 def iphonenum(txt): if len(txt) !=  12: return False for i in range(3): if not txt[i].isdecimal(): return False if txt[3] != '-': return False for j in range(4, 7): if not txt[j].isdecimal(): return False if txt[7] != '-': return False for l in range(8, 12): if not txt[l].isdecimal(): return False return True mes='Call me at 415-555-1101 tomorrow.415-666-7845 is my office.' for m in range(len(mes)): n = mes[m:m+12] if iphonenum(n): Print ('The phone number found is: ', n) Print ('find complete ')

3. Judge the domestic mobile phone number

 def iphonenum(txt): if len(txt) !=  11: return False for i in range(11): if not txt[i].isdecimal(): return False return True Print ('please enter mobile phone number: ', end=' ') k =input() Print ('determine whether it is a mobile phone number: ', iphonenum (k))

4. Regular expression
Example 1:

 import re phonenum = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d') k = 'my phone Number is 400-410-8888, and my office number is 411-808-9999·' mo = phonenum.search(k) Print ('print out the phone number found: ', mo. group()) print(mo.groups())

Example 2:

 import re phonenum = re.compile(r'(\d\d\d)-(\d\d\d-\d\d\d\d)') k = 'my phone Number is 400-410-8888, and my office number is 411-808-9999' mo = phonenum.search(k) print('1:', mo.group()) print('2:', mo.group(1)) print('3:', mo.group(2)) print('4:', mo.groups()) j,  l = mo.groups() print('5:', j) print('6:', l) m = mo.groups() n=list(m) print('7:', n) o=tuple(n) print('8:', o) p='-'.join(o) print('9:', p)

5. Grouping by pipeline matching

 import re ph = re.compile(r'Batman|Tina Fey') a = "Batman and Tina Fey" b = "Tina Fey and Batman" mo1 = ph.search(a) mo2 = ph.search(b) print('1:', mo1.group()) print('2:', mo2.group()) phx = re.compile(r'Bat(man|mil|cop|kk)') c = "Batman and Batmil and Batcop and Tina" mo3 = phx.search(c) print('3:', mo3.group()) print('4:', mo3.groups()) print('5:', mo3.group(1)) Print (''/'/////The following is the list converted from 4 and 5' '') d =list(mo3.groups()) print('6:',d) e =mo3.group(1) f = e.split() print('7:', f)

6. Question mark realizes optional matching

 import re px = re.compile(r'Bat(wo)? man') z = 'The Adventures of Batman' x = 'The Adventures of Batwoman' c = 'The Adventures of Batwowoman Batman' mo1 = px.search(z) print('1:', mo1.group()) print('1:', mo1.groups()) print('1:', mo1.group(1)) mo2 = px.search(x) print('2:', mo2.group()) print('2:', mo2.groups()) print('2:', mo2.group(1)) mo3 = px.search(c) print('3:', mo3.group()) print('3:', mo3.groups()) print('3:', mo3.group(1))

7. The asterisk realizes 0 or more matches

 import re px = re.compile(r'Bat(wo)*man') z = 'The Adventures of Batman' x = 'The Adventures of Batwoman' c = 'The Adventures of Batwowoman Batman' mo1 = px.search(z) print('1:', mo1.group()) print('1:', mo1.groups()) print('1:', mo1.group(1)) mo2 = px.search(x) print('2:', mo2.group()) print('2:', mo2.groups()) print('2:', mo2.group(1)) mo3 = px.search(c) print('3:', mo3.group()) print('3:', mo3.groups()) print('3:', mo3.group(1))

8. The plus sign realizes one or more matches

 import re px = re.compile(r'Bat(wo)+man') x = 'The Adventures of Batwoman' c = 'The Adventures of Batwowoman Batman' mo2 = px.search(x) print('2:', mo2.group()) print('2:', mo2.groups()) print('2:', mo2.group(1)) mo3 = px.search(c) print('3:', mo3.group()) print('3:', mo3.groups()) print('3:', mo3.group(1))

9. Curly brackets realize specific times of matching

 import re px = re.compile(r'Bat(wo) {3,5}man ') x = 'The Adventures of Batwowowoman' c = 'The Adventures of Batwowowowoman Batman' mo2 = px.search(x) print('2:', mo2.group()) print('2:', mo2.groups()) print('2:', mo2.group(1)) mo3 = px.search(c) print('3:', mo3.group()) print('3:', mo3.groups()) print('3:', mo3.group(1)) Print ('Python regular expressions are greedy by default and match the longest character in case of ambiguity ') 10. Usage of findall() import re phonenum = re.compile(r'\d\d\d-\d\d\d-\d\d\d\d') k = 'my phone Number is 400-410-8888, and my office number is 411-808-9999·411-785-1360,361-879-1360' mo = phonenum.findall(k) for i in range(len(mo)): k =mo[i] Print ('print the '+str (i+1)+' phone number found: ', k)

10. Character classification

 ''' \Number of d 0-9 \D Numbers other than 0-9 \W Any alphanumeric or underscore character (matching word characters) \W Any character except letters, numbers and underscores \S space, tab, newline (matches whitespace characters) \S Any character except space, tab and newline ''' print( ''' \Number of d 0-9 \D Numbers other than 0-9 \W Any alphanumeric or underscore character (matching word characters) \W Any character except letters, numbers and underscores \S space, tab, newline (matches whitespace characters) \S Any character except space, tab and newline ''') import re ce =re.compile(r'\d+\s+\w') le = re.compile(r'\d+\s') qe = re.compile(r'\d+') ke='11 text,23 your,89 kill' ve =ce.findall(ke) Print ('Output the list extracted from the string for the first time: ', ve) be = ' '.join(ve) Print ('convert list to string: ', be) ne =le.findall(be) Print ('Output the list extracted from the string for the second time: ', ne) me =[] for i in range(len(ne)): o =ne[i] p = o.strip() me.append(p) Print ('Output the list of numbers after removing spaces: ', me) tuple1 = tuple(me) Print ('tuple of output transformation: ', tuple1) str2=' '.join(tuple1) Print ('output string: ', str2) we = qe.findall(str2) Print ('convert to list again: ', we) ee = ''.join(we) Print ('link all numbers: ', ee)

11. Create your own character classification

 #Create your own character classification import re vowe =re.compile(r'[aeirAidR]') ve = 'Administrator,try agin' ce = vowe.findall(ve) print(ce)

12. Insert characters and dollar characters

 import re ne = re.compile(r'\d$') ve = re.compile(r'^Hello') ce = 'Hello World!' me = "my number is 42" be =ne.search(me) ae = ne.findall(me) print(be) print(ae) xe = ve.search(ce) ze= ve.findall(ce) print(xe) print(ze)

13. wildcard characters

 import re me = re.compile(r'.at') ne = 'the cat in the hat.sat on the flat mat' be = me.findall(ne) K=input ('Please add a word ending with at: ') be.append(k) print(be) ve =' '.join(be) ce=''.join(be) print(ve) print(ce)

14. Match all characters with dot star

 import re me = re.compile(r'First Name:(.*) Last Name:(.*)') ne = 'My First Name:Eric Last Name:Qiu' be =me.search(ne) print(be) print(be.group()) print(be.groups()) print(be.group(1)) print(be.group(2))

15 Period character matching newline

 import re me = re.compile(r'.*') ve =re.compile(r'.*', re.DOTALL) ne = 'My First Name:Eric \nLast Name:Qiu' be = me.search(ne) ce = ve.search(ne) print(be.group()) print(ce.group())

16. Case insensitive

 import re me1 = re.compile('RoboCop') me1 = re.compile('RObOCOP') me1 = re.compile('robOcop') me1 = re.compile('RobocOp') ne = re.compile(r'robocop', re.I) be = "RoboCop is part man.part machine,all cop" ve = ne.search(be) print(ve.group())

17. Use the sub() method to replace replacement characters

 import re ne = re.compile(r'Agent \w+') ve = ne.sub('CENSORED','Agent gave is part man.part machine,all cop') print(ve)