
02 Python高级 笔记
正则表达式
re 模块操作
-
re 模块的使用过程
1
2
3
4
5
6
7
8
9
# 导入 re 模块
import re
# 使用 match 方法进行匹配操作
result = re.match(正则表达式,要匹配的字符串)
# 如果上一步匹配到数据的话,可以使用 group 方法来提取数据
result.group()eg:
1
2
3
4
5
6
7
8
9
10import re
result = re.match("hello", "hello world")
print(result.group()) # hello
result = re.match("hello1", "hello world")
print(result.group())
# AttributeError: 'NoneType' object has no attribute 'group' -
re.match() 能够匹配出以 xxx 开头的字符串
匹配单字符

eg:
1 | import re |
1 | # \d 匹配 |
匹配多个字符

eg:
1 | # 匹配 * |
1 | # 匹配 + 号 |
1 | # 匹配 ? |
1 | # 匹配 {m} |
匹配开头结尾

eg:
1 | # 匹配 163.com 邮箱 |
本文是原创文章,采用CC BY-NC-SA 4.0协议,完整转载请注明来自coo1heisenberg's Blog






