正则表达式

正则表达式详解

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')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值