re模块主要应用是正则表达式。
1.常见正则表达式符号的应用
#match()从字符串开头匹配,'.'匹配除\n之外的任意一个字符,'+'
print(re.match("s.+","hiyi001233sipi007")) #None
print(re.match("h.+","hiyi001233sipi007")) # match='hiyi001233sipi007'
#search()从整个文本中搜索
print(re.search("s.+","hiyi001233sipi007")) #match='sipi007'
print(re.search("s.+i",'hiyi001233sipi007')) #match='sipi' i表示结尾
print(re.search("s[a-z]+0",'hiyi001233sipi007')) #match='sipi0' [a-z]表示26个小写
#注意下面4个的区别 在结尾之前不能有数字,若结尾是第一位数字则可以匹配,否则不行。
print(re.search("s[a-zA-Z]+c",'hiyi98suiytrTYUIc673')) #match='suiytrTYUIc'
print(re.search("s[a-zA-Z]+6",'hiyi98suiytrTYUIc673')) #match='suiytrTYUIc6'
print(re.search("s[a-zA-Z]+7",'hiyi98suiytrTYUIc673')) #None
print(re.search("s[a-zA-Z]+c",'hiyi98suiy4trTYUIc673')) #None
#'?'匹配前一个字符1次或0次
print(re.search("h?","hiyi")) #match='h'
print(re.search("h?","ihhuy")) #match=''
print(re.search("h?","hhhiyi")) #match='h'
print(re.search("hh?","hiyi")) #match='h'
print(re.search("hh?","hhhhiyi")) #match='hh'
print(re.search("hhh?","hhiyi")) #match='hh'
print(re.search("hhh?","hiyi")) #None
print(re.search("hhh?","hiyihhh")) #match='hhh'
print(re.search("hhh?","hhiyihhh")) #match='hh'
#{m}表示匹配前一种字符m次
print(re.search("[0-9]{3}","hiyi001233sipi007")) #match='001'
print(re.search("abc{2}","hiyi001233sipiabcc007")) #match='abcc' 匹配abc,c匹配2次
print(re.search("(abc){2}","hiyi001233sipiabcabcc007")) #match='abcabc'
print(re.search("(abc){2}\|","hiyi001233sipiabcabc|c007"))#match='abcabc|' '\'表示转义
print(re.search("(abc){2}(\|\|=){2}","hiyi001233sipiabcabc||=||=c007")) #match='abcabc||=||='
#{n,m}表示匹配前一种字符n到m次
print(re.search("[0-9]{1,3}",'hiyi001233sipi007')) #match='001'
print(re.search("[0-9]{1,3}",'hiyi0ti01233sipi007')) #match='0'
print(re.findall("[0-9]{1,3}","hiyi0ti01233sipi007"))#['0', '012', '33', '007']
#'|'匹配它左侧或右侧的字符。(从字符串头开始匹配,一旦匹配到左侧或右侧字符就停止)
print(re.search("123|abc","123675abcADG")) #match='123'
print(re.search("123|abc","ab123675cADG")) #match='123'
print(re.search("123|abc","abc123675cADG")) #match='abc'
#\A,\Z,\D,\W,\s,\w字符用法
print(re.search("\A[0-9]+[a-z]","1233abc")) # match='1233a' \A表示z只从字符串头开始匹配
print(re.search("\A[0-9]","ABC0765")) #None
print(re.search("[0-9]+[A-Z]\Z","abc8765WD")) #None
print(re.search("\D+","ASGHJIK-@9876")) #match='ASGHJIK-@' \D表示匹配非数字
print(re.search("\w","#$%afghA234")) #match='a' \w表示匹配[A-Za-z0-9]
print(re.search("\W","afghA234$%af"))#match='$' \W表示匹配非[A-Za-z0-9]
print(re.search("\s+","afhyjiu87\t34\n4")) # match='\t' \s表示匹配转义字符\t,\n,\r,和空白字符
print(re.search("\s+","afhyjiu87\t\n4")) #match='\t\n'
#'\Z'表示匹配前一个字符形式作为结尾,且\Z前面有且只能有一种字符
print(re.search("[0-9]\Z","AGHJKIU8876")) #match='6'
print(re.search("[0-9]\Z","AGHJKIU8")) #match='8'
print(re.search("[0-9]+[A-Z]\Z","AGHJKIU8345HJ")) #None
#分组匹配
a=re.search("(?P<id>[0-9]+)","aghjui45879HUY").group()
print(a) #45879
b=re.search("(?P<id>[0-9]+)","aghjui45879HUY").groupdict()
print(b) #{'id': '45879'}
c=re.search("(?P<ID>[0-9]+)(?P<NAME>[A-Z]+)","aghjui45879HUY").groupdict()
print(c) #{'NAME': 'HUY', 'ID': '45879'}
print(c['ID']) #45879 按照字典取值的方式
d=re.search("(?P<ID>[0-9]+)(?P<NAME>[A-Z]+)","aghjui45879HUY").group('ID')
print(d) #45879 有多个元素的直接取值方式
e=re.search("(?P<ID>[0-9]+)(?P<NAME>[a-z]+)","aghjui45879HUY").groupdict()
print(e) #无结果
当然:'^'匹配字符开头。'$'匹配字符结尾
split用法
#split用法 以第一个参数作为分割符,分割第二个参数字符串
print(re.split("[0-9]","5hjiy78yuw27spi8yu")) #['', 'hjiy', '', 'yuw', '', 'spi', 'yu']
print(re.split("[0-9]","hjiy78yuw27spi8yu")) #['hjiy', '', 'yuw', '', 'spi', 'yu']
print(re.split("[a-z]","hjiy78yuw27spi8yu")) #['', '', '', '', '78', '', '', '27', '', '', '8', '', '']
print(re.split("[0-9]+","hjiy78yuw27spi8yu")) #['hjiy', 'yuw', 'spi', 'yu']
#分割符为多个不同字符
print(re.split("[#_$]","hjiy#78yu_w27s$pi8yu")) #['hjiy', '78yu', 'w27s', 'pi8yu']
sub用法
#sub用法 第一个参数被替换字符,第二个参数替换后字符,第三个参数为要进行替换的字符串,第四个参数(可有可无)为替换次数
print(re.sub("[0-9]+","1","hyui2783uiytghyt3678opyu7yuhg")) #hyui1uiytghyt1opyu1yuhg
print(re.sub("[0-9]+","1","hyui2783uiytghyt3678opyu7yuhg",count=2)) #yui1uiytghyt1opyu7yuhg
正则表达式一般不用第三个参数,但也可以有,第三个参数通常会有额外的含义
#正则需要了解的用法
#第三个参数如果有 ,re.I(re.IGNORECASE)表示忽略大小写
print(re.search("[a-z]+","ghjAHGJKqwt",flags=re.I)) #match='ghjAHGJKqwt'
#第三个参数是re.S可以改变.行为,可以匹配\n
print(re.search(r".+","\t\n678yut\nabd",flags=re.S)) #match='\t\n678yut\nabd'
总结:re模块重要的5个函数match(),search(),findall(),split(),sub().
match()从字符串头开始匹配
search()从整个字符串中匹配
findall()搜索所有符合要求的字符
split()分割字符串
sub()替换字符串某些字符