Python3-正则表达式~pattern.sub

import  re

#替换
pattern = re.compile(r'(\w+) (\w+)')
s = "hello 123,hello 456"
s_list=pattern.findall(s)
print(s_list)

s_list=pattern.sub('hello world',s)
print(s_list)
 
 

/Library/Frameworks/Python.framework/Versions/3.6/bin/python3.6 /Users/apple/PycharmProjects/stage4/spider/2018——0307/re_sub.py
[('hello', '123'), ('hello', '456')]
hello world,hello world


Process finished with exit code 0

猜你喜欢

转载自blog.csdn.net/zbrj12345/article/details/80254446