Python正则表达式

【1】如果需要在一个字符串中删除某些字符串,可以用正则表达式:

 

import sys,re

t='a2b3c4zhhahhho12/13 222a5555(fuck)669aaaaaa9'

rep=r'2|3|4|hh|[0-9]+/[0-9]+|9.*9|\(.*\)'

t= re.sub(rep,'',t)

print t


规则[0-9]+/[0-9]+用来删除12/13

 

规则9.*9用来删除9aaaaaa9

规则\(.*\)用来删除(fuck)

【2】

 

mathch=re.compile('abc(.*)def').search('ddabcXXXXXXXXXdef')

print mathch.group(0)

print mathch.group(1)


结果:

 

 

abcXXXXXXXXXdef

XXXXXXXXX


 

 

你可能感兴趣的:(python)