re模块
re.compile(正则表达式) - 编译正则表达式,创建正则表达式对象
re_obj
= re
.compile(r
'\d{3}')
re
.fullmatch
(r
'\d{3}','234')
a
= re_obj
.fullmatch
('234')
print(a
)
re
.search
(r
'\d{3}','123321bjidionb3h21')
b
= re_obj
.search
('123321bjidionb3h21')
print(b
)
匹配
1) fullmatch(正则表达式,字符串) - 让整个字符串和正则表达式进行匹配
2)match(正则表达式,字符串) - 匹配字符串开头
如果匹配不到结果,就返回None,如果匹配成功了,结果式匹配的对象
re_str
= r
'\d{3}'
print(re
.fullmatch
(re_str
,'123'))
print(re
.fullmatch
(re_str
,'123qwe'))
print(re
.match
(re_str
,'123sdas'))
匹配对象
result
= re
.match
(re_str
,'133ewdsaad')
re_str
= r
'(\d{2})-([a-z]{3})'
result
= re
.match
(re_str
,'23-sjmhuikuandsuiabda')
print(result
)
1)获取到匹配到的字符串:
匹配对象.group() print(result.group())
匹配对象.group(分组号) - 获取正则表达式中指定的分组匹配到的结果(分组号从1开始)
print(result
.group
(1))
print(result
.group
(2))
2) 获取匹配到子串的范围
print(result
.span
(2))
3)获取原字符串
print(result
.string
)
查找
1)search(正则表达式,字符串) - 在字符串中查找第一个能和正则表达式匹配的子串。如果找不着返回的是None
str1
= '1hu2io32iub32,u1ibdu1321!iuob321io'
print(re
.search
(r
'\d+',str1
))
2)findall(正则表达式,字符串) - 获取字符串中所有满足正则表达式的子串,返回一个列表,列表中的元素是字符串
result
= re
.findall
(r
'\d+',str1
)
print(result
)
result
= re
.findall
(r
'(\d+)[a-z]',str1
)
print(result
)
result
= re
.findall
(r
'(\d+)([a-z])',str1
)
print(result
)
result
= re
.findall
(r
'\d+[a-z]',str1
)
print(result
)
3)finditer(正则表达式,字符串) - 获取字符串中所有满足正则表达式的子串
str2
= '1o1oabc===2o2pabc123hio3n12jnabc==!!!!'
result
= re
.findall
(r
'(\d[a-zA-Z]){2}abc',str2
)
print(result
)
result
= re
.finditer
(r
'(\d[a-zA-Z]){2}abc',str2
)
print(list(result
))
for i
in result
:
print(i
.group
(1))
切割
split(正则表达式,字符串) - 将字符串中能和正则表达式匹配的字符串作为切割点,对字符串进行切割,返回值是列表,列表中元素是字符串
str2
= 'asnd1n2io1ndo1n21odb21ondo31ds'
result
= re
.split
(r
'\d+',str2
)
print(result
)
替换
ub(正则表达式,字符串1,字符串2) - 将字符串2中能和正则表达式匹配的子串全部替换成字符串1
str2
= 'asnd1n2io1ndo1n21odb21ondo31ds'
result
= re
.sub
(r
'\d+','*',str2
)