赞
踩
. : 匹配任意一个字符(除了\n)
[] : 匹配列表中的字符
\d : 匹配数字,即0-9
\D : 匹配非数字
\s : 匹配空白、即空格(\n,\t)
\S : 匹配非空格
\w : 匹配单词字符,即a-z,A-Z,0-9,_
\W : 匹配非单词字符
. 示例
import re v1 = re.match('.', 'a') v2 = re.match('.', '1') v3 = re.match('.', '_') v4 = re.match('.', '\n') print(v1) print(v2) print(v3) print(v4) 》》》 <re.Match object; span=(0, 1), match='a'> <re.Match object; span=(0, 1), match='1'> <re.Match object; span=(0, 1), match='_'> None
\d示例
import re v1 = re.match('\d', '1') v2 = re.match('\d', 'a') v3 = re.match('\d', '_') v4 = re.match('\d', '\n') print(v1) print(v2) print(v3) print(v4) 》》》 <re.Match object; span=(0, 1), match='1'> None None None
\D示例
import re v1 = re.match('\D', '1') v2 = re.match('\D', 'a') v3 = re.match('\D', '_') v4 = re.match('\D', '\n') print(v1) print(v2) print(v3) print(v4) >>> None <re.Match object; span=(0, 1), match='a'> <re.Match object; span=(0, 1), match='_'> <re.Match object; span=(0, 1), match='\n'>
\s示例
import re v1 = re.match('\s', '1') v2 = re.match('\s', 'a') v3 = re.match('\s', '_') v4 = re.match('\s', '\n') print(v1) print(v2) print(v3) print(v4) 》》》 None None None <re.Match object; span=(0, 1), match='\n'>
\S示例
import re v1 = re.match('\S', '1') v2 = re.match('\S', 'a') v3 = re.match('\S', '_') v4 = re.match('\S', '\n') print(v1) print(v2) print(v3) print(v4) >>> <re.Match object; span=(0, 1), match='1'> <re.Match object; span=(0, 1), match='a'> <re.Match object; span=(0, 1), match='_'> None
\w示例
import re v1 = re.match('\w', '1') v2 = re.match('\w', 'a') v3 = re.match('\w', '_') v4 = re.match('\w', '\n') print(v1) print(v2) print(v3) print(v4) 》》》 <re.Match object; span=(0, 1), match='1'> <re.Match object; span=(0, 1), match='a'> <re.Match object; span=(0, 1), match='_'> None
\W示例
import re v1 = re.match('\W', '1') v2 = re.match('\W', 'a') v3 = re.match('\W', '_') v4 = re.match('\W', '\n') print(v1) print(v2) print(v3) print(v4) >>> None None None <re.Match object; span=(0, 1), match='\n'>
[ ]示例
# 匹配手机号 import re v1 = re.match('\d\d\d\d\d\d\d\d\d\d\d', '12345678912') v2 = re.match('1[35789]\d\d\d\d\d\d\d\d\d', '18217752311') v3 = re.match('1[35789]\d\d\d\d\d\d\d\d\d', '11217752311') print(v1) print(v2) print(v3) 》》》 <re.Match object; span=(0, 11), match='12345678912'> <re.Match object; span=(0, 11), match='18217752311'> None
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。