栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

python正则表达式d 匹配数字D 匹配非数字w 匹配单词W 匹配非单词字符

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python正则表达式d 匹配数字D 匹配非数字w 匹配单词W 匹配非单词字符

 一个d代表一个数字。开头没匹配到,即使字符串其他部分包含需要匹配的内容,.match也会返回none

import re
a = re.match('dd','23es12testasdtest')
print(a)
print(a.group())
b = re.match('ddd','23es12testasdtest')
print(b)   #要求匹配三个数字,匹配不到返回none
c = re.match('d','es12testasdtest')
print(c)   #起始位置没有匹配成功,一样返回none

输出: 


23
None
None

D 匹配非数字

开头没匹配到,即使字符串其他部分包含需要匹配的内容,.match也会返回none

import re
a = re.match('D','23es12testasdtest')
print(a)     #开头为数字所以返回none
b = re.match('DD','*es12testasdtest')
print(b)   #返回*e
print(b.group()) 

输出: 

None

*e
 s 匹配特殊字符,如空白,空格,tab等
import re
a=re.match('s',' 23es 12testasdtest')
print(a)
print(a.group())
print(re.match('s',' 23es 12testasdtest'))   #匹配空格
print(re.match('s','   23es 12testasdtest')) #匹配tab
print(re.match('s','r23es 12testasdtest')) #匹配r换行
print(re.match('s','23es 12testasdtest')) #返回none

输出


 



None
S 匹配非空白 
import re
print(re.match('S',' 23es 12testasdtest'))   #返回none
print(re.match('S','r23es 12testasdtest'))   #none
print(re.match('S','23es 12testasdtest'))
a=re.match('S','23es 12testasdtest')
print(a.group()) 

输出:

None
None

2

w 匹配单词、字符,如大小写字母,数字,_ 下划线 (除了空格)
import re
print(re.match('w','23es 12testasdtest'))
a=re.match('w','23es 12testasdtest')
print(a.group())
print(re.match('www','aA_3es 12testasdtest'))
b=re.match('www','aA_3es 12testasdtest')
print(b.group()) 
print(re.match('www','n12testasdtest'))   #返回none

 输出:


2

aA_
None

W 匹配非单词字符(与上面那个是相对的)(只有空格)
import re
print(re.match('W','23es 12testasdtest'))   #返回none
print(re.match('W',' 23es 12testasdtest'))   #匹配空格
a=re.match('W',' 23es 12testasdtest')
print(a.group())

输出: 

 

[ ] 匹配[ ]中列举的字符(只保留一位)

只允许出现[ ]中列举的字符

import re
print(re.match('12[234]','232s12testasdtest'))  #因为开头的12没匹配上,所以直接返回none
print(re.match('12[234]','1232s12testasdtest')) #返回123
a=re.match('12[234]','1232s12testasdtest')
b=re.match('12[234]','122454223432s12testasdtest')
c=re.match('12[234]','123562223432s12testasdtest')
d=re.match('12[234]','124223432s12testasdtest')
print(a.group())
print(b.group())
print(c.group())
print(d.group())

输出:

None

123
122
123
124
[^2345] 不匹配2345中的任意一个 
import re
print(re.match('12[^234]','232s12testasdtest'))  #因为开头的12没匹配上,所以直接返回none
print(re.match('12[^234]','1232s12testasdtest')) #返回none
print(re.match('12[^234]','1252s12testasdtest')) #返回125
a=re.match('12[^234]','1252s12testasdtest')
print(a.group())

None
None

125
[a-z3-5] 匹配a-z或者3-5中的字符
import re
print(re.match('12[1-3a-c]','1232b12testasdtest'))
print(re.match('12[1-3a-c]','1222b12testasdtest')) #123
print(re.match('12[1-3a-c]','1231b12testasdtest'))
a=re.match('12[1-3a-c]','1232b12testasdtest')
print(re.match('12[1-3a-c]','12b2b12testasdtest'))  #12b
print(re.match('12[1-3a-c]','12a2b12testasdtest'))
print(re.match('12[1-3a-c]','12c2b12testasdtest'))
b=re.match('12[1-3a-c]','12b2b12testasdtest')
print(re.match('12[1-3a-c]','12s2b12testasdtest'))  #返回none
print(a.group())
print(b.group())

 输出:






None
123
12b

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/861321.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号