将字符串转换为Python中所有非字母数字字符的列表

时间:2014-03-26 03:13:06

标签: python regex list

如何扫描字符串中的所有非字母数字字符?

到目前为止,我有以下内容:

pattern = re.compile('\W')

2 个答案:

答案 0 :(得分:3)

这将给出一组非字母数字符号:

set(string.printable) - set(string.letters) - set(string.digits)

或使用RE:

re.findall("\W", string.printable)

答案 1 :(得分:0)

使用标准库字符串方法扫描字符串的示例。

http://docs.python.org/2/library/stdtypes.html#string-methods

charlist = ['google', 'stack-overflow', 'twitter', '#$!word']
["".join([y for y in x if not y.isalpha()]) for x in charlist]
out[1]: ['', '-', '', '#$!']