一个\d代表一个数字。开头没匹配到,即使字符串其他部分包含需要匹配的内容,.match也会返回none
import rea = re.match('\d\d','23es12testasdtest')print(a)print(a.group())b = re.match('\d\d\d','23es12testasdtest')print(b) #要求匹配三个数字,匹配不到返回nonec = re.match('\d','es12testasdtest')print(c) #起始位置没有匹配成功,一样返回none
输出:
<re.Match object; span=(0, 2), match='23'>23NoneNone
\D 匹配非数字
开头没匹配到,即使字符串其他部分包含需要匹配的内容,.match也会返回none
import rea = re.match('\D','23es12testasdtest')print(a)#开头为数字所以返回noneb = re.match('\D\D','*es12testasdtest')print(b) #返回*eprint(b.group())
输出:
None<re.Match object; span=(0, 2), match='*e'>*e
\s 匹配特殊字符,如空白,空格,tab等
import rea=re.match('\s',' 23es 12testasdtest')print(a)print(a.group())print(re.match('\s',' 23es 12testasdtest')) #匹配空格print(re.match('\s',' 23es 12testasdtest')) #匹配tabprint(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 reprint(re.match('\S',' 23es 12testasdtest')) #返回noneprint(re.match('\S','\r23es 12testasdtest')) #noneprint(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 reprint(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 reprint(re.match('\W','23es 12testasdtest')) #返回noneprint(re.match('\W',' 23es 12testasdtest')) #匹配空格a=re.match('\W',' 23es 12testasdtest')print(a.group())
输出:
[ ] 匹配[ ]中列举的字符(只保留一位)
只允许出现[ ]中列举的字符
import reprint(re.match('12[234]','232s12testasdtest')) #因为开头的12没匹配上,所以直接返回noneprint(re.match('12[234]','1232s12testasdtest')) #返回123a=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'>123122123124
[^2345] 不匹配2345中的任意一个
import reprint(re.match('12[^234]','232s12testasdtest')) #因为开头的12没匹配上,所以直接返回noneprint(re.match('12[^234]','1232s12testasdtest')) #返回noneprint(re.match('12[^234]','1252s12testasdtest')) #返回125a=re.match('12[^234]','1252s12testasdtest')print(a.group())
NoneNone<re.Match object; span=(0, 3), match='125'>125
[a-z3-5] 匹配a-z或者3-5中的字符
import reprint(re.match('12[1-3a-c]','1232b12testasdtest'))print(re.match('12[1-3a-c]','1222b12testasdtest')) #123print(re.match('12[1-3a-c]','1231b12testasdtest'))a=re.match('12[1-3a-c]','1232b12testasdtest')print(re.match('12[1-3a-c]','12b2b12testasdtest')) #12bprint(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')) #返回noneprint(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'>None12312b
如果觉得《python正则表达式\d 匹配数字\D 匹配非数字\w 匹配单词\W 匹配非单词字符》对你有帮助,请点赞、收藏,并留下你的观点哦!