python 正则表达式查找标题之间的文本

发布于 2025-01-16 09:48:57 字数 291 浏览 1 评论 0原文

考虑一下文本:

标题

标题

标题
鸟儿

使用 python 尝试构建一个正则表达式来提取每个 HEADER 部分之间的文本。

我已经尝试过:

results = re.findall(r'HEADER([\s\S]+)(?:HEADER||$))

这最终会捕获捕获组中的所有文本。

假设上面的正则表达式将捕获标题块或标题块和文件末尾之间的所有文本

Consider the text:

HEADER
the cat
HEADER
the dog
HEADER
the bird

Using python attempting to construct a regex to extract the text between each HEADER section.

I have tried:

results = re.findall(r'HEADER([\s\S]+)(?:HEADER||$))

This ends up capturing all the text in the capture group.

Assumption was the above regex would capture all text between to header blocks, or a header block and the end of file

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离笑几人歌 2025-01-23 09:48:57

我觉得在这种特定情况下使用 .split("HEADER") 比正则表达式更容易。您是否出于某种原因特别需要正则表达式?

I feel that in that specific case using a .split("HEADER") is easier then a regex. Do you specifically need a regex for some resason?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文