1

I'm trying to make a very simple regex match ( the negation of all printable anscii characters other then '[' and ']'). When I tested my pattern in regex pal I got the matching that I wanted. Then I moved to my python unit test and my match never returns 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)

Which is returning

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

re.match is always returning none for some reason.

UPDATE: tried to change my code in the suggested fashion new output is

False
False
True
False
False
False

1 Answer 1

2
def testChatChars(string):
   return re.match(r'[\x20-\x5A\x5C\x5E-\x7E]+$', string) is not None
Sign up to request clarification or add additional context in comments.

3 Comments

This worked for me. Why do I not need a ^ character and what does the +$ symbol do?
@Rawrgulmuffins, I recommend you read docs.python.org/2/howto/regex.html .
fair enough, I just made the (apparently wrong) assumption that python regex was perl regex.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.