python strip()的正则表达式版本

写一个函数,它接受一个字符串,做的事情和 strip()字符串方法一样。如果只传入了要去除的字符串,没有其他参数,那么就从该字符串首尾去除空白字符。否则,函数第二个参数指定的字符将从该字符串中去除。

import re

def REstrip(text, param=' '):
	mo = re.compile(r'^([' + str(param) + r']*)(.*?)([' + str(param) + ']*)$')
	result = mo.search(text)
	if (result != None):
		print(result.group(2))


text = input("Please input the text: ")
param = input("Please input the param: ")
REstrip(text, param)

猜你喜欢

转载自blog.csdn.net/dongyu1703/article/details/81782081
今日推荐