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

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

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

输出: 

<re.Match object; span=(0, 2), match='23'>
23
None
None

\D 匹配非数字

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

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

输出: 

None
<re.Match object; span=(0, 2), match='*e'>
*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

输出

<re.Match object; span=(0, 1), match=' '>
 
<re.Match object; span=(0, 1), match=' '>
<re.Match object; span=(0, 1), match=' '>
<re.Match object; span=(0, 1), match='\r'>
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
<re.Match object; span=(0, 1), match='2'>
2

\w 匹配单词、字符,如大小写字母,数字,_ 下划线 (除了空格)

import re
print(re.match('\w','23es 12testasdtest'))
a=re.match('\w','23es 12testasdtest')
print(a.group())
print(re.match('\w\w\w','aA_3es 12testasdtest'))
b=re.match('\w\w\w','aA_3es 12testasdtest')
print(b.group()) 
print(re.match('\w\w\w','\n12testasdtest'))   #返回none

 输出:

<re.Match object; span=(0, 1), match='2'>
2
<re.Match object; span=(0, 3), match='aA_'>
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
<re.Match object; span=(0, 3), match='123'>
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
<re.Match object; span=(0, 3), match='125'>
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())

 输出:

<re.Match object; span=(0, 3), match='122'>
<re.Match object; span=(0, 3), match='123'>
<re.Match object; span=(0, 3), match='12b'>
<re.Match object; span=(0, 3), match='12a'>
<re.Match object; span=(0, 3), match='12c'>
None
123
12b

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值