如何匹配字符串的开头,结尾和中间的特殊符号?
我知道,我应该使用正则表达式.例如,我做了一个功能:
def check_word(word: str) -> bool:
special_symbols = '()1234567890!?_@#$%^&*.,'
e = re.match(special_symbols, word) # How to match symbols at end?
m = re.match(special_symbols, word) # How to match symbols at middle?
s = re.match(special_symbols, word) # How to match symbols at start?
if e is not None:
return True
if m is not None:
return True
if s is not None:
return True
if e is not None and s is not None:
return False
if s is not None and m is not None:
return False
print(check_word("terdsa223124")) # --> True
print(check_word("ter223124dsa")) # --> True
print(check_word("223124terdsa")) # --> True
print(check_word("223124terdsa223124")) # --> False
print(check_word("223124ter223124dsa")) # --> False
如何填写re.match以便打印正确?