正则表达式:捕获线后没有'---'

问题描述 投票:3回答:2

这里是一个小文本,看起来像restructuredText。

This is a sentence to catch.

Title that should not be caught
-------------------------------

Another sentence to catch.

我想要一个正则表达式,它捕获不是标题的两行,并保留is标题的句子。

  1. 测试1:如何离开---行。我已经完成了/^(?!(---))[^\n]+/gm。它将行留在标题下。它给了我:
This is a sentence to catch.
Title that should not be caught
Another sentence to catch.
  1. 测试2:如何在行标题(Title that should not be caught)上方保留句子?我尝试过/^(?!(---))[^\n]+(?!\n---)/gm,这给了我:
This is a sentence to catch.
Title that should not be caugh
Another sentence to catch.

问题是它没有抓住前面的字母 \n---呼唤我想抓住的没有<< [整个句子。我想的是:

This is a sentence to catch. Another sentence to catch.
我该怎么办?

编辑:

感谢您的回复效果很好(我不确定是否了解所有内容,但我会考虑一下……)。

如果您同意,让我们进一步扩展该问题。新玩具示例:

This is another title not to catch, Ha ! ======================================== This is a sentence to catch. Title that should not be caught ------------------------------- Another sentence to catch.

如您所见,我在===行中添加了另一种标题。使用Tân的正则表达式,我得到:

======= This is a sentence to catch. Another sentence to catch.

    测试1之二:我已经测试过.+(?![\w\s\n-=]+).+,但没有发现任何问题:(
  • 仅供参考,我正在Parsimonious在python上实现一些东西。
  • regex restructuredtext
    2个回答
    1
    投票
    您可以尝试通过编程语言使用此模式:.+(?![\w\s\n-]+).+

    0
    投票
    如果您使用的是Linux(特别是Ubuntu),则可以尝试以下操作:
    © www.soinside.com 2019 - 2024. All rights reserved.