1、match()和search()两个方法的区别?
import re
word
= "Hello World!!!"
print(re
.match
(r
"World", word
))
print(re
.search
(r
"World", word
))
2、如何通过序号和标识来获取想要匹配的某一段内容?
r
= re
.compile("H(.{2})lo (.{5})")
print(re
.match
(r
, word
).groups
())
print(re
.match
(r
, word
).group
())
print(re
.match
(r
, word
).group
(1))
print(re
.match
(r
, word
).group
(2))
r1
= re
.compile("(?P<name>Hello) (?P<name1>World)")
print(re
.search
(r1
, word
).group
("name1"))
3、如何对字符串中的数字进行降维打击?
def double(matched
):
value
= int(matched
.group
('value'))
return str(value
/ 2)
s
= 'A23G4HFD567'
print(re
.sub
(r
'(?P<value>\d+)', double
, s
))
转载请注明原文地址:https://tech.qufami.com/read-2233.html