我感觉用举例说明比较容易懂:
表示边界
字符 功能
^ 匹配字符串开头
$ 匹配字符串结尾
\b 匹配一个单词的边界
\B 匹配非单词边界
示例1:$
需求:匹配163.com的邮箱地址
import re citing
=re.match(“[\w]{4,20}@163.com$”,’fasfsafsaf@163.com’).group()
print(citing)
输出是:fasfsafsaf@163.com
示例2: \b
import re
citing = re.match(r”.*\bworld\b”,”hallo world i”).group()
print(citing)
输出是:hallo world
示例3:\B
import re
citing = re.match(r”.*\Bworld\B”,”halloworldi”).group()
print(citing)
输出是:halloworld
查找一起的,有空个就会报错
匹配分组
字符 功能
| 匹配左右任意一个表达式
(ab) 将括号中字符作为一个分组
\num 引用分组num匹配到的字符串
(?P) 分组起别名
(?P=name) 引用别名为name分组匹配到的字符串
示例1:|
需求:匹配出0-100之间的数字
import re
citing = re.match(“[1-9]?\d$|100”,”78”).group()
print(citing)
输出:78
示例2:( )
需求:匹配出163、126、qq邮箱之间的数字
import re
citing = re.match(“\w{4,20}@(163|126|qq).com”, “test@qq.com”).group()
print(citing)
输出:test@qq.com