python - 正则表达式最小匹配
问题描述
我正在尝试在 Python 中编写一个正则表达式,它将匹配包含特定单词的单个句子(从.
到)。.
我写的正则表达式是r"\..*?hello.*?\."
. 如您所见,我正在使用非贪婪量词。我匹配的文本是:
This is the 1st sentence. This is the 2nd. This is the 3rd, hello, world. This is the 4th.
(这里是regex101的链接)
我希望匹配为This is the 3rd, hello, world.
,但尽管*?
量词是非贪婪的,但匹配还包括第二句。我错过了什么?
解决方案
推荐阅读
- python - 在 Xcode 11.5 中构建 kivy-ios 应用程序失败并出现 Python 语法错误
- python-3.x - 如何从用户那里获取输入并将其传递给由python中的子进程调用触发的交互式命令行程序?
- javascript - onChange 不适用于移动设备?- 反应原生
- grpc - 使用 grpc 的 2 个微服务之间的通信如何工作?
- service - 如何在 /iwfnd/maint_service 中生成系统别名?
- sql - SQL根据参数从表中排除行
- elasticsearch - Elasticsearch - 节点如何检测分片故障
- python - Seaborn 绘图未显示
- python - 将数据帧中的样本输入 ARMA 模型并获得“ValueError:设置带有序列的数组元素。”
- python - 在 c++ 中导入用 swig 包装的 python 模块的正确方法是什么