Python regex for finding all words in a string [duplicate]

青春壹個敷衍的年華 提交于 2020-01-01 05:05:08

问题


Hello I am new into regex and I'm starting out with python. I'm stuck at extracting all words from an English sentence. So far I have:

import re

shop="hello seattle what have you got"
regex = r'(\w*) '
list1=re.findall(regex,shop)
print list1

This gives output:

['hello', 'seattle', 'what', 'have', 'you']

If I replace regex by

regex = r'(\w*)\W*'

then output:

['hello', 'seattle', 'what', 'have', 'you', 'got', '']

whereas I want this output

['hello', 'seattle', 'what', 'have', 'you', 'got']

Please point me where I am going wrong.


回答1:


Use word boundary \b

import re

shop="hello seattle what have you got"
regex = r'\b\w+\b'
list1=re.findall(regex,shop)
print list1

OP : ['hello', 'seattle', 'what', 'have', 'you', 'got']

or simply \w+ is enough

import re

shop="hello seattle what have you got"
regex = r'\w+'
list1=re.findall(regex,shop)
print list1

OP : ['hello', 'seattle', 'what', 'have', 'you', 'got']


来源:https://stackoverflow.com/questions/37543724/python-regex-for-finding-all-words-in-a-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!