regex - 正则表达式 '[\w-]+(\.[\w-]+)*' 不匹配
问题描述
我想处理PostgreSQL文档中的一些句子并做一些分析。在分词阶段,我尝试使用Lotufo 等人提出的正则表达式'[\w-]+(.[\w-]+)*' 。在文章Modeling the Hurried bug report 阅读过程中总结了 bug 报告。很奇怪,我在 Python 中使用这个正则表达式无法得到预期的答案。
Python 3.6.5 |Anaconda, Inc.| (default, Mar 29 2018, 13:32:41) [MSC v.1900 64 bit (AMD64)]
Type "copyright", "credits" or "license" for more information.
IPython 6.4.0 -- An enhanced Interactive Python.
>>> import re
>>> result = re.findall(r'[\w-]+(\.[\w-]+)*', 'Specifies the directory to use for data storage.')
>>> print(result)
我期望得到一个单词列表:</p>
['Specifies', 'the', 'directory', 'to', 'use', 'for', 'data', 'storage']
但我只有一个空字符串列表:
['', '', '', '', '', '', '', '']
有谁知道我的代码有什么问题?非常感谢。
解决方案
这按您期望的方式工作:
Python 3.7.2 (default, Jan 16 2019, 19:49:22)
[GCC 8.2.1 20181215 (Red Hat 8.2.1-6)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import re
>>> split = re.compile('(\w+)')
>>> split.findall('Specifies the directory to use for data storage.')
['Specifies', 'the', 'directory', 'to', 'use', 'for', 'data', 'storage']
>>>
正则表达式上的那些方括号感觉不对。我想他们是原因。
推荐阅读
- npm - 无法使用 npm 安装 firebase CLI
- python - Python:如何根据条件制作组合字段
- java - openVPN 在 androidstudio 中无法在 Android 10 上运行
- r - if (is.double(data$x) && !has_groups(data) && any(data$x != data$x[1L])) { 中的错误:需要 TRUE/FALSE 的缺失值
- c++ - 单元测试回声功能
- javascript - 为什么 JavaScript 有盒装版本的原语?
- python - 网络摄像头 openCV 错误:(-215) ssize.width > 0 && ssize.height > 0 in function resize
- python - numpy select TypeError: unhashable type: 'list - numpy 需要列表
- node.js - 如何使用 facebook 令牌或 google accesstoken 使用 nodejs 或 lambda 函数注册 AWS cognito?
- electron - 通过 ElectronJs 中的子菜单项关闭当前窗口