Python正则表达式函数
正则表达式函数:正则表达式函数有:re.match()函数、re.search()函数、全局匹配函数、re.sub()函数
- match():从头开始匹配
string = "Poythonpty" pat = "p.*?y" pat1 = "y.*" rst = re.match(pat, string, re.I) rst1 = re.match(pat1, string, re.I) print(rst) print(rst1)
打印结果:
- search():任意地方匹配,从左到右提取一个结果
string = "Poythonpty" pat = "p.*?y" rst = re.search(pat, string, re.I) print(rst)
打印结果:
- 全局匹配函数:全局匹配格式:re.compile(正则表达式).findall(数据)
string = "PoythonptyohphyjkPdsy" pat = "p.*?y" rst = re.compile(pat, re.I).findall(string) print(rst)
打印结果:
- 常见实例
实例1:.com和.cn网址
string = "< a helf=‘https://www.baidu.com‘>百度首页</a>" # pat = ‘http.*.com‘ pat = "[a-zA-Z]+:[^\s]*[.com|.cn]" # [a-zA-Z]+ :任意字符开头 [^\s]* :非空字符多次匹配 [.com|.cn] :原子表中包含.com或.cn result = re.compile(pat).findall(string) print(result)
打印结果:
实例2:匹配电话号码
string = "shhgy020-876380287jku0768-8937690789e132514512012" pat = "\d{4}-\d{7}|\d{3}-\d{8}|1\d{10}" rst = re.compile(pat).findall(string) print(rst)
打印结果:
相关推荐
flyingssky 2020-08-18
wangzhaotongalex 2020-10-20
wyq 2020-11-11
TLROJE 2020-10-26
风雨断肠人 2020-10-13
duanqingfeng 2020-09-29
rechanel 2020-11-16
cshanzhizi 2020-10-16
luofuIT成长记录 2020-09-22
phphub 2020-09-10
taomengxing 2020-09-07
MaggieRose 2020-08-19
山水沐光 2020-08-18
jyj00 2020-08-15
AHuqihua 2020-08-09
山水沐光 2020-08-03