我正在使用这个 re.match 调用来获取“正确”的字符串:
re.match('^[A-Za-z0-9\.\,\:\;\!\?\(\)]', str)
但我也得到了一些垃圾,比如#
and _
。这怎么可能?我究竟做错了什么?
谢谢!
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 *