python正则

import re

# findall
content = 'this is a test falg!!!'
pattern = re.compile('a (.*?) falg', re.S)
ret = re.findall(pattern, content)
print(ret)
['test']
# sub-----re.sub(pattern, repl, string, count, flags)
content = 'this is a test falg!!!'
ret = re.sub('a (.*?) falg', 'a big flag', content)
print(ret)
this is a big flag!!!
# replace-----指定明确的old和new
content = 'this is a test falg!!!'
ret = content.replace('test', 'big')
print(ret)
this is a big falg!!!
原文地址:https://www.cnblogs.com/hui-code/p/13922774.html