python正则表达式

#1、python中使用正则表达式步骤:
import re #导入正则表达式模块
# ff = re.compile(r'\d{3}-\d{3}-\d{4}') #用compile函数创建一个Regex对象
# gg = ff.search('my phone is:075-333-4466') #像Regex对象的search()方法传入想查找的字符串,他返回一个match对象
# print(gg.group()) #调用match对象的group()方法,返回实际匹配文本的字符串

# 2、用正则表达式匹配更多模式
# 1.利用括号分组
# phone = re.compile(r'(\d\d\d\d)-(\d\d\d-\d\d\d)')
# mo = phone.search('my phone is:0755-998-3366.')
# print(mo.group(1)) #0755 第一组
# print(mo.group(2)) #998-336 第二组
# print(mo.group()) #0755-998-336 所有分组
# print(mo.group(0)) #0755-998-336 所有分组
# print(mo.groups()) #('0755', '998-336') 所有分组
# aa,bb=mo.groups() #groups返回多个值的元组,所以可以使用多重复制的技巧
# print(aa) #0755
# print(aa) #998-336
# 2.用管道匹配多个分组,是“或”的关系,默认匹配先找到的(前者)
# hB = re.compile(r'Batom|tina fey')
# mo1=hB.search('Batom and tina fey')
# mo2=hB.search('tina fey and Batom')
# print(mo1.group()) #Batom
# print(mo2.group()) #tina fey
# 可以使用管道来匹配多个模式中的一个,作为正则表达式
# bat = re.compile(r'Bat(man|mobile|bat)')
# mo = bat.search('Batmobile list a wheel')
# print(mo.group()) #Batmobile
# print(mo.group(1)) #mobile
# 3.用问好实现可选匹配(无论这段文本在不在,正则表达式都会认为匹配)
# bat = re.compile(r'Bat(wo)?man') #匹配这个问好之前的分组零次或一次
# mo1=bat.search('the adventures of Batman')
# mo2=bat.search('the adventures of Batwoman')
# print(mo1.group()) #Batman
# print(mo2.group()) #Batwoman
# 4.用*号匹配零次或多次,*号之前的分组,可以在文本中出现任意次(可以一次都没有,可以重复多次)
# bat = re.compile(r'Bat(wo)*man') #匹配这个问好之前的分组零次或一次
# mo1=bat.search('the adventures of Batman')
# mo2=bat.search('the adventures of Batwowowowoman')
# print(mo1.group()) #Batman
# print(mo2.group()) #Batwowowowoman
# 5.用+号匹配一次或多次(至少出现一次)
# bat = re.compile(r'Bat(wo)+man')
# mo1 = bat.search('the adventures of Batman')
# print(mo1==None) #True
# 6.用{}(花括号)匹配特地次数 ha{3}
# ha = re.compile(r'(ha){3}')
# mo1 = ha.search('hahaha')
# mo2 = ha.search('haha')
# print(mo1.group()) #hahaha
# print(mo2==None) #True

# 3、贪心模式和非贪心模式。python的正则表达式默认是贪心模式,这表示在二义的情况下,他们会尽可能匹配最长的字符串。
# greed = re.compile(r'(ha){3,5}') #最少匹配3次最多匹配5次
# mo1 = greed.search('hahahahahaha')
# print(mo1.group()) #hahahahaha
#
# greed = re.compile(r'(ha){3,5}?') #?声明非贪心模式
# mo1 = greed.search('hahahahahaha')
# print(mo1.group()) #hahaha

# 4、findall()方法(返回所有匹配,返回的是一个字符串列表):
# 1.如果调用在一个没有分组的正则表达式上,如\d\d-\d\d-\d\d ,方法findall()将返回一个匹配字符串的列表 如['22-33-44','55-66-66']
# 2.如果调用在一个有分组的正则表达式上,如(\d\d)-(\d\d),方法findall()将返回一个字符串的元组的列表(每个分组对应一个字符串),如[('22','33'),('88'-'99')]
# phone = re.compile(r'\d\d-\d\d')
# mo = phone.findall('my phone:22-55,33-66')
# print(mo) #['22-55', '33-66']
#
# phone = re.compile(r'(\d\d)-(\d\d)')
# mo1 = phone.findall('my phone:22-55,33-66')
# print(mo1) #[('22', '55'), ('33', '66')]

# 5、字符分类
# \d #0到9的任何数字
# \D #除0到9的数字以外的任何字符
# \w #任何字母、数字或下划线字符(可以认为是匹配“单词”字符)
# \W #除字母、数字、下划线以外的任何字符
# \s #空格、制表符或换行符(可以认为是匹配“空白”字符)
# \S #除空格、制表符和换行符以外的任何字符
posted @ 2017-09-14 15:57  13684995613  阅读(189)  评论(0编辑  收藏  举报