strip()的正则表达式版本

题目:写一个函数,它接受一个字符串,做的事情和 strip()字符串方法一样。如果只

传入了要去除的字符串,没有其他参数,那么就从该字符串首尾去除空白字符。

否则,函数第二个参数指定的字符将从该字符串中去除。

答案:

import re
str1 = input('请输入字符串:')
str2 = input('请输入要删除的字符,若无请按回车:')
def func(str1,str2):
    mo1 = re.compile(r'^s*')
    mo2 = re.compile(r's*$')
    mo3 = re.compile(str2)
    re1 = mo1.sub('',str1)
    re2 = mo2.sub('',re1)
    re3 = mo3.sub('',re2)
    print(re3)
func(str1,str2)
原文地址:https://www.cnblogs.com/huzhikai001/p/12822162.html