python - 在python中匹配单词到单词的内容
问题描述
我想正则表达式从 python 列表中的内容,
内容是这样的:
garbage text
garbage text
Heading 1:
important content abx
important content xvy
important content
important content xyz
important content zed
Heading 2:
more content
我想将内容从“标题 1”抓取到“zed”
我已经尝试过的事情:
regex = r'Heading 1.*?zed'
但它不适用于多行
解决方案
尝试re.findall
在全点模式下使用以下模式:
\bHeading 1:.*?(?=Heading \d+|$)
全部点模式将确保跨行.*?
匹配。结尾的前瞻将在下一个标题开始之前或输入结束之前停止,以先发生者为准。
示例脚本:
inp = """garbage text
garbage text
Heading 1:
important content abx
important content xvy
important content
important content xyz
important content zed
Heading 2:
more content"""
matches = re.findall(r'\bHeading 1:.*?(?=Heading \d+|$)', inp, flags=re.DOTALL)
print(matches[0])
这打印:
Heading 1:
important content abx
important content xvy
important content
important content xyz
important content zed
推荐阅读
- github - 在 Github 中使用“使用此模板”创建存储库时,请更新 README
- google-sheets - 如何从另一个谷歌工作簿的参考中获取特定单元格?
- sql - 需要帮助在 Oracle SQL 查询中将行转换为列
- python - 如何在 Tensorflow 中找到神经网络模型梯度的最小点?
- wpf - SQL 重新连接实例 (WPF)
- node.js - Express - 使用多嵌套路由器
- xslt - 如何创建副本然后更新 xslt 中的 1 个标签?
- nestjs - 使用 NestJS Sequelize 访问多对多中的关联模型
- java - 当应用程序处于后台时,android线程一直滞后
- tabulator - 如何在制表器中居中列标题?