Python 模式表达与正则表达式
1.国外手机判断
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('请输入手机号码:', end='')
k =input()
print('判断是否为手机号码:', iphonenum(k))
2.查找文本中的手机号码
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('找到的电话号码是:', n)
print('查找完成')
3.判断国内手机号码
def iphonenum(txt):
if len(txt) != 11:
return False
for i in range(11):
if not txt[i].isdecimal():
return False
return True
print('请输入手机号码:', end='')
k =input()
print('判断是否为手机号码:', iphonenum(k))
4.正则表达式
例一:
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('打印出找到的电话号码:', mo.group())
print(mo.groups())
例二:
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.用管道匹配分组
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(''' ////////以下是4和5转换为的列表''''')
d =list(mo3.groups())
print('6:',d)
e =mo3.group(1)
f = e.split()
print('7:', f)
6.问号实现可选匹配
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.星号实现0个或多个匹配
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.加号实现一个或多个匹配
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.花括号实现特定次数匹配
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正则表达式默认是贪心的,在有二义的情况匹配最长字符 ')
10.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('打印出找到的第'+str(i+1)+'个电话号码:', k)
10.字符分类
'''
\d 0-9的数字
\D 除0-9以外的数字
\w 任何数字字母或下划线字符(匹配单词字符)
\W 除字母,数字和下划线以外的任何字符
\s 空格,制表符,换行符(匹配空白字符)
\S 除空格,制表符,换行符以外的任何字符
'''
print(
'''
\d 0-9的数字
\D 除0-9以外的数字
\w 任何数字字母或下划线字符(匹配单词字符)
\W 除字母,数字和下划线以外的任何字符
\s 空格,制表符,换行符(匹配空白字符)
\S 除空格,制表符,换行符以外的任何字符
''')
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('输出第一次从字符串中提取的列表:', ve)
be = ' '.join(ve)
print('把列表转换为字符串:', be)
ne =le.findall(be)
print('输出第二次从字符串中提取的列表:', ne)
me =[]
for i in range(len(ne)):
o =ne[i]
p = o.strip()
me.append(p)
print('输出去空格过后的数字列表:', me)
tuple1 = tuple(me)
print('输出转换的元组:', tuple1)
str2=' '.join(tuple1)
print('输出字符串:', str2)
we = qe.findall(str2)
print('再次转换为列表:', we)
ee = ''.join(we)
print('把所有数字链接:', ee)
11.建立自己的字符分类
#建立 自己的字符分类
import re
vowe =re.compile(r'[aeirAidR]')
ve = 'Administrator,try agin'
ce = vowe.findall(ve)
print(ce)
12.插入字符和美元字符
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.通配字符
import re
me = re.compile(r'.at')
ne = 'the cat in the hat.sat on the flat mat'
be = me.findall(ne)
k =input('请加入一个以at结尾的单词:')
be.append(k)
print(be)
ve =' '.join(be)
ce=''.join(be)
print(ve)
print(ce)
14.用点-星匹配所有字符
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句点字符匹配换行
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.不区分大小写
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.使用sub()方法替换替换字符
import re
ne = re.compile(r'Agent \w+')
ve = ne.sub('CENSORED','Agent gave is part man.part machine,all cop')
print(ve)
本作品采用 知识共享署名-相同方式共享 4.0 国际许可协议 进行许可。
朋友 交换链接吗
朋友 有专门的友链页