import re
def Strip(str_1, str_2= ''):
if str_2:
stripRegex = re.compile(str_2)
mo = stripRegex.sub('', str_1)
return mo
else:
stripRegex = re.compile(r'^(\s*)(.*)(\s*)$')
mo = stripRegex.search(str_1)
return mo.group(2)
print(Strip(' abasdfa '))
print(Strip(' abasdfa ', 'a'))
《python编程快速上手——让繁琐工作自动化》7.18.3 strip()的正则表达式
最新推荐文章于 2024-07-17 09:15:39 发布