正则表达式(补充)

前文

定义:

正则表达式是对字符串操作的一种逻辑公式,用事先定义好的一些特定字符、以及特定字符的组合,组成一个‘规则字符串’,‘规则字符串’用来表达对字符串的一种过滤逻辑

正则表达式非python独有,在python中,re模块实现正则表达式

在线正则表达式测试

使用在线正则表达式测试样例:

import re

#常规匹配
content = 'hello 123 4567 world_this is a re demo'
result = re.match('^hello\s\d\d\d\s\d{4}\s\w{10}.*demo',content)
print(result)
输出结果:
<_sre.SRE_Match object; span=(0, 38), match='hello 123 4567 world_this is a re demo'>
#泛匹配
content = 'hello 123 4567 world_this is a re demo'
result = re.match('^hello.*demo$',content)
print(result)

  

 

猜你喜欢

转载自www.cnblogs.com/ronghe/p/9162482.html