'''如果只传入了要去除的字符串,没有其他参数,那么就从该字符串首尾去除空白字符。
否 则,函数第二个参数指定的字符将从该字符串中去除'''
import re
def new_strip(str_1,str_2=''):
if str_2:
SentenceRegex=re.compile(str_2)
sentence1=SentenceRegex.sub('',str_1)
print(sentence1)
else:
blank=re.compile(r'( )(.*)( )')
blank1=blank.search(str_1)
print(blank1.group(2))
new_strip('A apple a day keep docter away!','day')
new_strip(' wolf can eat meat,but dog just eat shit! ')
strip()的正则表达式版
最新推荐文章于 2024-04-01 15:19:19 发布