#strip()的正则表达式版本
import re
def regular_strip(text, found_text=''):
if found_text:
regex = re.compile(found_text)
return regex.sub('', text)
else:
regex = re.compile(r'^\s+|\s+$')
return regex.sub('', text)
text = ' sffv vd+ '
found_text = ''
print(regular_strip(text, found_text))
text = ' sffvvd+ '
found_text = 'v'
print(regular_strip(text, found_text))
打印结果
sffv vd+
sffd+
[Finished in 0.2s]