I am using this re.match call to get only "proper" strings:
re.match('^[A-Za-z0-9\.\,\:\;\!\?\(\)]', str)
But I am getting some garbage too, like #
and _
. How is that possible? What am I doing wrong?
Thanks!
I am using this re.match call to get only "proper" strings:
re.match('^[A-Za-z0-9\.\,\:\;\!\?\(\)]', str)
But I am getting some garbage too, like #
and _
. How is that possible? What am I doing wrong?
Thanks!
Use this to check all characters until the end of your string, otherwhise your pattern will only check the first character:
re.match('^[A-Za-z0-9.,:;!?()]+$', str)
Note that the character class doesn't contain spaces, newlines or tabs. You can add them like this:
re.match('^[A-Za-z0-9.,:;!?()\s]+$', str)
If you want to allow void strings you can replace the +
quantifier by *