import re
#正则表达式
#构建正则表达式
pattern = re.compile('hello')
#match()函数只检测RE是不是在string的开始位置匹配,
#search()会扫描整个string查找匹配;
#也就是说match()只有在0位置匹配成功的话才有返回,
#如果不是开始位置匹配成功的话,match()就返回none。
res1 = re.match(pattern,'hello')
res2 = re.match(pattern,'hello CQC')
res3 = re.match(pattern,'helo')
res4 = re.match(pattern,'Hello')
res5 = re.search(pattern,'world hello')
res6 = re.match(pattern,'world hello')
print res1
print res2
print res3 #None
print res4 #None
print res5
print res6 #None
#其他匹配
reg = re.compile('^he') #开头是he
reg1 = re.compile('jack$') #结尾是jack
print re.search(reg,'he is jack')
print re.search(reg1,'he is jack')
#[a-z][1-9] \w 匹配字母数字及下划线
reg3 = re.compile('\w{2}') #查找到两个字母数字或下划线
print re.search(reg3,'__ab13')
#单词边界\b,默认是退格字符,单词边界时,加上r'\b'
reg4 = re.compile(r'\bis{2}\b')
print re.search(reg4,'his name is is jack')
reg = re.compile('[a-z]\d[a-z]{3}') #一个字母一个数字再加3个字母,即a1bbb
print re.search(reg,'a1bbb2c')
reg5 = re.compile('([a-z]\d[a-z]){2}') #连续出现两次 一个字母一个数字1个字母,匹配3个,即a1bc2de3f
print re.search(reg5,'a1bb2cd3f') #匹配有没有
print re.findall(reg5,'a1bb2cd3f') #['b2c']
reg6 = re.compile('([a-z]\d[a-z])') #一个字母一个数字1个字母,即a1bc2de3f
print re.search(reg6,'a1bb2cd3f')
print re.findall(reg6,'a1bb2cd3f') #['a1b', 'b2c', 'd3f']
#[a-z][1-9] \w 匹配字母数字及下划线
reg3 = re.compile('\d{2,4}') #查找到2到4个数字
print re.search(reg3,'__ab134')
print re.findall(reg3,'__ab134')
reg = re.compile('\d+') #+号表示一个及以上数字
print re.search(reg,'abc1')
email = re.compile('^[a-zA-Z0-9]+\w+@\w+\.com$') #表示”点“要加”\“转义符
print re.search(email,'a@qq.com')
正则表达式
最新推荐文章于 2022-09-01 08:29:07 发布