【发布时间】:2017-07-24 07:50:34
【问题描述】:
我正在尝试清理字符串,使其没有任何标点符号或数字,它必须只有 a-z 和 A-Z。 例如,给定 String 为:
"coMPuter scien_tist-s are,,, the rock__stars of tomorrow_ <cool> ????"
需要的输出是:
['computer', 'scientists', 'are', 'the', 'rockstars', 'of', 'tomorrow']
我的解决办法是
re.findall(r"([A-Za-z]+)" ,string)
我的输出是
['coMPuter', 'scien', 'tist', 's', 'are', 'the', 'rock', 'stars', 'of', 'tomorrow', 'cool']
【问题讨论】:
-
您最好的解决方案是使用简单的替换来删除所有不是 a-z 和空格的字符。
[^A-Za-z ]+(你可以用\s代替右括号后面的空格,然后用空格作为分隔符对字符串进行拆分。在正则表达式中,你可以 -
您能详细说明一下吗?
-
@cfqueryparam 谢谢你在说什么 re.sub( r'([^a-zA-Z\s]+)', '', s).split()
标签: python regex string python-2.7