python - 如何使用 python regex 提取上下文的第一部分
问题描述
string='there is a article here, there will be some other article too'
字符串是我要的内容,'there is a article'
就停在那里,不匹配最后的文章。
我用过there.+article
,但它给了我完整的内容'there is a article here, there will be some other article
。不,我不想要这个。
有我想要的:
'there is a article'
解决方案
您可以使用?
非贪婪匹配。
>>> string='there is a article here, there will be some other article too'
>>> import re
>>> re.match("there.+article", string)
<re.Match object; span=(0, 57), match='there is a article here, there will be some other>
>>> re.match("there.+?article", string)
<re.Match object; span=(0, 18), match='there is a article'>
推荐阅读
- javascript - 为什么当我滚动到顶部然后在我的 API 应用程序上返回但在网页上正常运行时,我的页面顶部按钮口吃/滞后/粘住?
- html - 制作多个时HTML超链接不起作用
- r - 根据条件仅将 R 中的某些行转换为绝对值
- android - 如何将笔画添加到材质按钮的底部?
- javascript - Javascript在页面加载时更新标签的href
- typescript - 类型'文章| null' 不可分配给类型 'void'
- python - 我如何使不和谐的机器人不区分大小写?不和谐.py
- javascript - pagination.js 在 webpack rails 中不起作用
- python - 在guizero应用程序中按下Enter键时如何调用函数?
- nand2tetris - 破解程序集:添加 R3、R1、R9,给出错误“预期表达式”