你好,我是新进入regex和我开始与python。 我一直致力于从一个英语句子中提取所有的单词。 到目前为止我有:
import re
shop="hello seattle what have you got"
regex = r'(\w*) '
list1=re.findall(regex,shop)
print list1
这将提供输出:
['hello', 'seattle', 'what', 'have', 'you']
如果我用
regex = r'(\w*)\W*'
然后输出:
['hello', 'seattle', 'what', 'have', 'you', 'got', '']
我想要这个输出
['hello', 'seattle', 'what', 'have', 'you', 'got']
请告诉我哪里出错了。
使用单词边界
\b
或者只要
\w+
就足够了相关问题 更多 >
编程相关推荐