保定网站建设seo优化营销,湖南官网网站推广软件,福建漳州网站建设费用,智慧团建系统手机端目录
1. 基础匹配
2. 元字符匹配 1. 基础匹配 正则表达式#xff0c;又称规则表达式#xff08;Regular Expression#xff09;#xff0c;是使用单个字符串来描述、匹配某个句法规则的字符串#xff0c;常被用来检索、替换那些符合某个模式#xff08;规则#xff…
目录
1. 基础匹配
2. 元字符匹配 1. 基础匹配 正则表达式又称规则表达式Regular Expression是使用单个字符串来描述、匹配某个句法规则的字符串常被用来检索、替换那些符合某个模式规则的文本。 简单来说正则表达式就是使用字符串定义规则并通过规则去验证字符串是否匹配。 比如验证一个字符串是否是符合条件的电子邮箱地址只需要配置好正则规则即可匹配任意邮箱。比如通过正则规则 (^[\w-](\.[\w-])*[\w-](\.[\w-])$) 即可匹配一个字符串是否是标准邮箱格式。但如果不使用正则使用if else来对字符串做判断就非常困难了。 Python正则表达式使用re模块并基于re模块中三个基础方法来做正则匹配。分别是match、search、findall 三个基础方法。 # 要导入re模块
import res hello Python ,you should study hardres re.match(hello,s)
print(res) # re.Match object; span(0, 5), matchhello
# 可以通过span方法看到匹配的下标范围
print(res.span()) # (0,5)
# 通过group方法可以看到匹配的内容
print(res.group()) # hello
import res 1hello Python ,you should study hardres re.match(hello,s)
print(res) #None
# 不能以为是s里面有hello就能匹配
# match是跟s的头部匹配头部不匹配就不继续往后看 import res hello Python ,you can say hellores1 re.search(hello,s)
print(res1) # re.Match object; span(0, 5), matchhellores2 re.search(tell,s)
print(res2) # None import res hello Python ,you can say hellores1 re.findall(hello,s)
print(res1) # [hello, hello]res2 re.findall(tell,s)
print(res2) # []
2. 元字符匹配 正则最强大的功能在于元字符匹配规则。 import res hello Python 123456163.com# 字符串的r标记表示当前字符串是原始字符串
# 即内部的转义字符无效,而是普通字符
res1 re.findall(r\d, s)
print(res1) # [1, 2, 3, 4, 5, 6, 1, 6, 3]res2 re.findall(r[a-zA-Z],s)
print(res2)
# [h, e, l, l, o, P, y, t, h, o, n, c, o, m] import re
str c5d252dD
# 设置匹配规则只能是字母或数字且长度限制在6~10位
r ^[0-9a-zA-Z]{6,10}$
print(re.findall(r,str)) # [c5d252dD]
# 返回结果不为空说明符合条件
import re
str 03254265
# 设置匹配规则要求纯数字长度5-11第一位不为0
# 第一个设置[1,9]说明第一位数字不为0
# 第二个设置[0,9]说明内容必须是纯数字
# 第三个设置{4,10}因为我们已经设置了第一位的范围所以剩余长度就是4-10
# ^表示从头开始找$表示找到尾
r ^[1,9][0,9]{4,10}$
print(re.findall(r,str)) # []
# 返回结果为空说明不符合条件
import re
# 匹配邮箱地址只允许qq、163、gmail这三种邮箱地址
# [\w-]表示匹配单词字符和-
# 表示匹配的字符出现1到无数次
# (\.[\w-])* 匹配零个或多个以点(.)开头后跟一个或多个单词字符
# 用于分隔用户名和域名
# (qq|163|gmail)匹配“qq”、“163”或“gmail”中的任意一个
rr(^[\w-](\.[\w-])*(qq|163|gmail)(\.[\w-])$)
s a.b.c.d.e.f.gqq.com.a.z.c.d.e
# 对于findall如果r内部用了()进行分组
# 那么findall会把每一个满足内部规则的内容返回
# 因此我们要在最外面再加上一个()用来找到满足内部所有规则的结果
print(re.findall(r,s))
# [(a.b.c.d.e.f.gqq.com.a.z.c.d.e, .g, qq, .e)]