正则表达式

re模块

在线验证工具:http://regexr.com/

练习:https://alf.nu/RegexGolf

import re
pattern=re.compile(r'hello.*\!')
match=pattern.match('hello, hanxiaoyang! How are you?')
if match:
    print(match.group())
 1 import re
 2 m=re.match(r'(\w+) (\w+)(?P<sign>.*)','hello hanxiaoyang!')
 3 print(m.string) #hello hanxiaoyang!
 4 print(m.re) #re.compile('(\\w+) (\\w+)(?P<sign>.*)')
 5 print(m.pos)#0
 6 print(m.endpos)#18
 7 print(m.lastindex)#3
 8 print(m.lastgroup)#sign
 9 
10 print('')
11 print(m.group(1,2))#('hello', 'hanxiaoyang')
12 print(m.groups())#('hello', 'hanxiaoyang', '!')
13 print(m.groupdict())#{'sign': '!'}
14 print(m.start())#0
15 print(m.end())#18
16 print(m.span())#(0, 18)
17 print(m.expand(r'\2 \1\3'))#hanxiaoyang hello!
View Code

search匹配子串

import re
p=re.compile(r'H.*g')
#match表示从开始匹配,search匹配子串
match=p.search('hello Hanxiaoyang!')
if match:
    print(match.group()) #Hanxiaoyang
View Code

split分割

import re
p=re.compile(r'\d+')
print(p.split('one1two2three3four4')) #['one', 'two', 'three', 'four', '']

findall:全部匹配子串

import re
p=re.compile(r'\d+')
print(p.findall('one1two2three3four4')) #['1', '2', '3', '4']

finditer:返回一个顺序访问每一个匹配结果(Match对象)的迭代器

import re
p=re.compile(r'\d+')
for m in p.finditer('one1two2three3four4'):
    print(m.group())
    '''
    1
    2
    3
    4
    '''
View Code

sub:替换

import re
p=re.compile(r'(\w+) (\w+)')
s='i say,hello hanxiaoyang!'
print(p.sub(r'\2 \1',s)) #say i,hanxiaoyang hello!

def func(m):
    return m.group(1).title()+' '+m.group(2).title()

print(p.sub(func,s)) #I Say,Hello Hanxiaoyang!

subn:返回替换次数

import re
p=re.compile(r'(\w+) (\w+)')
s='i say,hello hanxiaoyang!'
print(p.subn(r'\2 \1',s)) #('say i,hanxiaoyang hello!', 2)

def func(m):
    return m.group(1).title()+' '+m.group(2).title()

print(p.subn(func,s)) #('I Say,Hello Hanxiaoyang!', 2)

 

posted @ 2018-03-05 10:05  88aa123  阅读(141)  评论(0编辑  收藏  举报