python正则表达式2
匹配电话号码:
1 >>> import re 2 >>> pattern=r'\d{3,4}-?\d{8}' 3 >>> re.findall( pattern, '021-12345678' ) 4 ['021-12345678'] 5 >>> re.findall( pattern, '02188888888' ) 6 ['02188888888'] 7 >>> re.findall( pattern, '0218888888' ) 8 [] 9 >>> re.findall( pattern, '021-8888888' ) 10 [] 11 >>>
把正则表达式,编程成一个对象,可以提高匹配效率
>>> patterTel=re.compile( pattern ) >>> patterTel <_sre.SRE_Pattern object at 0x7fd19ba5ab90> >>> patterTel.findall( '021-88888888' ) ['021-88888888'] >>>
可以通过编译成对象,传参(re.I),把匹配方式改成忽略大小
>>> pattern=r'[a-z]+' >>> re.findall( pattern, 'abc' ) ['abc'] >>> re.findall( pattern, 'aBc' ) ['a', 'c'] >>> re.compile( pattern, re.I ) <_sre.SRE_Pattern object at 0x7fd19b906490> >>> cPattern = re.compile( pattern, re.I ) >>> cPattern.findall( 'aBc' ) ['aBc'] >>>
match,匹配到的字符是否在 被匹配字符串的开头,如果是,返回一个对象.
>>> pattern=r'abc' >>> re.match( pattern, 'ghostwu abc' ) >>> re.match( pattern, 'abc ghostwu' ) <_sre.SRE_Match object at 0x7fd19b96c780> >>> re.match( pattern, ' abc ghostwu' ) >>> res = re.match( pattern, 'abc ghostwu' ) >>> if res: ... print '是以abc开头的' ... else: ... print '不是以abc开头的' ... 是以abc开头的 >>>
search:存在就返回对象
>>> pattern=r'abc' >>> re.search( pattern, 'abc ghostwu' ) <_sre.SRE_Match object at 0x7fd19b96c850> >>> re.search( pattern, 'ghostwu abc' ) <_sre.SRE_Match object at 0x7fd19b96c780> >>> re.search( pattern, 'ghostwu abc def' ) <_sre.SRE_Match object at 0x7fd19b96c850> >>> re.search( pattern, 'ghostwu def' ) >>>
finditer返回一个迭代器,findall返回的是列表
>>> pattern=r'abc' >>> iterator=re.finditer( pattern, 'abc def abc ghostwu abc' ) >>> iterator.next() <_sre.SRE_Match object at 0x7fd19b96c780> >>> iterator.next() <_sre.SRE_Match object at 0x7fd19b96c850> >>> iterator.next() <_sre.SRE_Match object at 0x7fd19b96c780> >>> iterator.next() Traceback (most recent call last): File "<stdin>", line 1, in <module> StopIteration >>>
group方法,可以查看match匹配到的值
>>> pattern=r'abc' >>> res=re.match( pattern, 'abc ghostwu abc' ) >>> res <_sre.SRE_Match object at 0x7fd19b96c850> >>> res.group() 'abc'
同样可用于迭代器返回的对象
>>> pattern=r'abc' >>> re.finditer( pattern, 'abc ghostwu abc abc hello' ) <callable-iterator object at 0x7fd19b982190> >>> res = re.finditer( pattern, 'abc ghostwu abc abc hello' ) >>> res.next().group() 'abc' >>> res.next().group() 'abc' >>> res.next().group() 'abc' >>> res.next().group() Traceback (most recent call last): File "<stdin>", line 1, in <module> StopIteration >>>
re.sub:替换
>>> str='hello abc' >>> str.replace( 'abc', 'ghostwu' ) 'hello ghostwu' >>> str 'hello abc' >>> re.sub( r'abc', 'ghostwu', str ) 'hello ghostwu' >>>
把小写的(r,s,t)字母换成大写的R
>>> str='ghostwu:hi,nice to meet you!' >>> re.sub( r'[r-t]', 'R', str ) 'ghoRRwu:hi,nice Ro meeR you!' >>>
>>> str=r'jfsdaft jdfasfcxvt jfdsafdast' >>> pattern=r'j.*t?' >>> re.sub( pattern, 'javascript', str ) 'javascript' >>> re.subn( pattern, 'javascript', str ) ('javascript', 1)
subn:结果中多了一个数字,表示一共替换了多少次
split:切割
>>> ip='127.0.0.1' >>> ip.split( '.' ) ['127', '0', '0', '1'] >>> pattern='\.' >>> re.split( pattern, ip ) ['127', '0', '0', '1']
>>> str="hello, my name is ghostwu" >>> pattern=r'[\s,]' >>> re.split( pattern, str ) ['hello', '', 'my', 'name', 'is', 'ghostwu'] >>>
如果分割符号的形式不同,用正则的split是比较方便的
相关推荐
RuoShangM 2020-02-27
Buerzhu 2019-12-02
dailinqing 2019-10-28
chenwen0000 2019-09-07
shijinling0 2019-06-29
LHpython 2019-04-23
Python探路者 2019-04-18
JamesHammer 2019-04-17
PythonBiglove 2018-12-25
JamesHammer 2019-04-05
pythonpycharm 2013-12-25
yoyoshadow 2013-11-20