Python正则表达式

来源:互联网 发布:vscode 代码高亮插件 编辑:程序博客网 时间:2024/05/15 06:36
【1】如果需要在一个字符串中删除某些字符串,可以用正则表达式:

import sys,ret='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)

结果:

abcXXXXXXXXXdefXXXXXXXXX


原创粉丝点击