请问创造一个函数input两个变量,如何在re.compile里面来表达这个变量?
#!python3
# deleteBlank.py - to delete the blank as same as strip() or delete the specific string
#create a string
import re
def deleteBlank(string,delete):
print('deleteBlank is to delete the blank at the start or end of the string as same as what strip do or delete the specific part in the string')
if delete=='':
#it works as same as the string.strip()
#find the string maybe start with the blank
blankRegex=re.compile(r'^(\s)*(.*)(\s)*$',re.DOTALL)
print(blankRegex.sub(r'\2',string))
else:
findRegex=re.compile(r'(.*)[delete](.*)',re.DOTALL)
print(findRegex.sub(r'\1\2',string))
deleteBlank('I love beautiful girls.','beautiful')
deleteBlank(' hello world ','')
结果是这样的
deleteBlank is to delete the blank at the start or end of the string as same as what strip do or delete the specific part in the string
I love beautiful girs.
deleteBlank is to delete the blank at the start or end of the string as same as what strip do or delete the specific part in the string
hello world