1

我正在使用这个 re.match 调用来获取“正确”的字符串:

re.match('^[A-Za-z0-9\.\,\:\;\!\?\(\)]', str)

但我也得到了一些垃圾,比如#and _。这怎么可能?我究竟做错了什么?

谢谢!

4

1 回答 1

4

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 *

于 2013-09-03T14:21:19.753 回答