簡體   English   中英

重新匹配總是在python中返回None

[英]Re.match always returning None in python

我試圖做一個非常簡單的正則表達式匹配(除'['和']'以外的所有可打印anscii字符取反)。 當我在正則表達式pal中測試我的模式時,我得到了想要的匹配項。 然后我移到了python單元測試,我的比賽從不返回true

def testChatChars(string):
   return re.match('[^\x20-\x5A\x5C\x5E-\x7E]', string) is not None

print("testing Chat validation") 
print(testChatChars("") == False)
print(testChatChars("this is a valid chat message") == True)
print(testChatChars("9999abcdefghijklmnopqrxtuvxyz ABCDEFGHIJKLMNOP!@#$(^&*(&%$^^)*)!{},.;'\|?/7") == True )
print(testChatChars("this is not [ valid chat message") == False)
print(testChatChars("this is not ] valid chat message") == False)
print(testChatChars("9999abcdefghijklmnopqrxtuvxyz [][][[][]ABCDEFGHIJKLMNOP!@#$(^&*(&%$^^)*)!{}[],.;'\|?/7ونِكود碼標準萬國") == False)

哪個回來了

False //should be true
False //should be true
False //should be true
True
True
True

由於某些原因,re.match始終不返回任何內容。

更新: 試圖以建議的方式更改我的代碼,新的輸出是

False
False
True
False
False
False
def testChatChars(string):
   return re.match(r'[\x20-\x5A\x5C\x5E-\x7E]+$', string) is not None

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM