TNT*_*TNT 7 python regex words sentence
你好,我是新的正则表达式,我开始使用python.我坚持从英语句子中提取所有单词.到目前为止,我有:
import re
shop="hello seattle what have you got"
regex = r'(\w*) '
list1=re.findall(regex,shop)
print list1
Run Code Online (Sandbox Code Playgroud)
这给出了输出:
['你好','西雅图','什么','有','你']
如果我替换正则表达式
regex = r'(\w*)\W*'
Run Code Online (Sandbox Code Playgroud)
然后输出:
['你好','西雅图','什么','有','你','有','']
而我想要这个输出
['你好','西雅图','什么','有','你','有']
请指出我哪里出错了.
使用单词边界 \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']
Run Code Online (Sandbox Code Playgroud)
或者仅仅\w+
是足够的
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']
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
23728 次 |
最近记录: |