Python编程快速上手 7.18.2 strip()的正则表达式版本
写一个函数,它接受一个字符串,做的事情和 strip() 字符串方法一样。如果只传入了要去除的字符串,没有其他参数,那么久从该字符串首位去除空白字符。否则,函数第二个参数置顶的字符将从该字符串中去除。
#! python3
import re
def stripRe():
print("请输入文本:")
text = input()
print("请输入去除的字符串,默认空格则直接按'Enter键'")
word = input()
if word == '':
word = ' '
#只去掉两边的空格
stripRegex = re.compile(r'([a-zA-Z0-0])+(.*)*([a-zA-Z0-9])+')
mo = stripRegex.search(text).group()
print(mo)
print('_______________')
else:
print(f'你输入的是:{word}')
stripRegex = re.compile(f'{word}')
print(stripRegex.sub('',text))
stripRe()