The string I'm testing can be matched with [\w-]+
. Can I test if a string conforms to this in Python, instead of having a list of the disallowed characters and testing for that?
To check whether a String contains only unicode letters or digits in Java, we use the isLetterOrDigit() method and charAt() method with decision-making statements. The isLetterOrDigit(char ch) method determines whether the specific character (Unicode ch) is either a letter or a digit.
The idea is to use the regular expression ^[a-zA-Z0-9]*$ , which checks the string for alphanumeric characters. This can be done using the matches() method of the String class, which tells whether this string matches the given regular expression.
Is a dash an alphanumeric character? The login name must start with an alphabetic character and can contain only alphanumeric characters and the underscore ( _ ) and dash ( – ) characters. Full name can contain only letters, digits, and space, underscore ( _ ), dash ( – ), apostrophe ( ' ), and period ( . ) characters.
[A-Za-z0-9] matches a character in the range of A-Z, a-z and 0-9, so letters and numbers. + means to match 1 or more of the preceeding token.
If you want to test a string against a regular expression, use the re library
import re valid = re.match('^[\w-]+$', str) is not None
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With