Checking for allowed characters

suggest change

If you want to check that a string contains only a certain set of characters, in this case a-z, A-Z, 0-9, and ., you can do so like this:

import re

def is_allowed(string):
    characterRegex = re.compile(r'[^a-zA-Z0-9.]')
    string = characterRegex.search(string)
    return not bool(string)
    
print (is_allowed("abyzABYZ0099")) 
# Out: True

print (is_allowed("#*@#$%^")) 
# Out: False

You can also adapt the expression line from [^a-zA-Z0-9.] to [^a-z0-9.], to disallow uppercase letters for example.

Partial credit : http://stackoverflow.com/a/1325265/2697955

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents