我正在嘗試測試一個字符是否是特殊字符。當我編寫以下代碼時,它因 '-' 字符而失?。篿mport res = '-'regex = re.compile('[!@#$%^&*()-+]')if regex.search(s) == None: print("Not found")else: print("Found")輸出>>未找到但是,如果我按如下方式更改模式中“-”字符的位置(代碼第 3 行),它可以正常工作import re s = '-'regex = re.compile('[!@#$%^&*()+-]')if regex.search(s) == None: print("Not found")else: print("Found")輸出>>找到是什么導致了這種差異,我如何確保能夠檢測到字符?
無法檢測正則表達式中的“-”字符
慕田峪7331174
2021-09-11 20:09:57