Python 正则表达式学习(4):查找以“one”结尾,前面包含0个或多个英文字母(不能是数字或其它字符)的字符串。

需求是:查找以“one”结尾,包含0个或多个英文字母(不能是数字或其它字符)的字符串。

import re

str='bacdone1cdonone345dhdfgkone'

p=re.compile(r'[a-zA-Z]*one')

#只搜索一次
m=re.match(p,str)
print(m.group())

#发现和此模式相匹配的所有字符串
mylist=p.findall(str)
print(" ".join(mylist))
print(mylist)

#以此模式分割字符串
subs=re.split(p,str)
for sub in subs:
    print(sub)
print(subs)

#用A替换此模式中的字符
print(re.sub(p,"A",str))

猜你喜欢

转载自blog.csdn.net/acflair/article/details/82708189