分类: Python/Ruby
2021-03-11 17:27:02
#
import re
##########.的使用###############
q = 'a'
w = 'A'
e = '_'
r = '\n'
pattern = '.'
q1 = re.match(pattern,q)
w1 = re.match(pattern,w)
e1 = re.match(pattern,e)
r1 = re.match(pattern,r)
print(q1,'\n',w1,'\n',e1,'\n',r1)
#########\d的使用###########
q = '0123'#由结果看只能匹配一个
w = '5'
e = 'a'
r = ' 9'
pattern = '\d'
q1 = re.match(pattern,q)
w1 = re.match(pattern,w)
e1 = re.match(pattern,e)
r1 = re.match(pattern,r)
print(q1,'\n',w1,'\n',e1,'\n',r1)
##########\s的使用#############
q = ' '
w = '\n'
e = '\t'
r = '_'
pattern = '\s'
q1 = re.match(pattern,q)
w1 = re.match(pattern,w)
e1 = re.match(pattern,e)
r1 = re.match(pattern,r)
print(q1,'\n',w1,'\n',e1,'\n',r1)
##############[]的使用#############
q = '2'
w = '3'
e = '4'
pattern = '[2468]'
q1 = re.match(pattern,q)
w1 = re.match(pattern,w)
e1 = re.match(pattern,e)
print(q1,'\n',w1,'\n',e1)
#################测试结果#########################################
None
None
None
None
None
Process finished with exit code 0
泽一匹配符(|)和列表
import re
#泽一和列表的相同点
pattern = '[xyz]'
pa1 = 'x|y|z'
s = 'z'
o = re.search(pattern,s)
o1 = re.search(pa1,s)
print(o,'\n',o1)
print('#########不同点#######')
#泽一和列表的不同点
pattern = '[ab][cd]'
pa1 = 'ab|cd'
s1 = 'bc'
s2 = 'ab'
o1 = re.search(pattern,s1)
o2 = re.search(pattern,s2)
a1 = re.search(pa1,s1)
a2 = re.search(pa1,s2)
print(o1,'\n',o2,'\n',a1,'\n',a2)
################结果##############################
#########不同点#######
None
None
Process finished with exit code 0