部分参考:https://www.cnblogs.com/edwardsun/p/4421773.html
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# encoding: UTF-8
import re
import
re
# 将正则表达式编译成Pattern对象
pattern = re.compile(r'world')
pattern
=
re.
compile
(r
'world'
)
# 使用search()查找匹配的子串,不存在能匹配的子串时将返回None
# 这个例子中使用match()无法成功匹配
match = pattern.search('hello world!')
match
pattern.search(
'hello world!'
if match:
if
match:
# 使用Match获得分组信息
print match.group()
print
match.group()
### 输出 ###
# world
参与评论
手机查看