正则表达式:捕获线后面没有“---”
Regex : Catch line not followed by '---'
这是一个看起来像重组文本的小文本。
This is a sentence to catch.
Title that should not be caught
-------------------------------
Another sentence to catch.
我想要一个正则表达式来捕获不是 header 的两行,并留下 是 的句子作为标题。
- 测试 #1:如何离开
---
行。我已经完成 /^(?!(---))[^\n]+/gm
。它将行留在 header 下。它给了我:
This is a sentence to catch.
Title that should not be caught
Another sentence to catch.
- 测试 #2:如何在 header (
Title that should not be caught
) 行上方也留下句子?我试过 /^(?!(---))[^\n]+(?!\n---)/gm
结果是:
This is a sentence to catch.
Title that should not be caught
Another sentence to catch.
问题是它没有捕捉到之前的字母 \n---
而我想要的是没有捕捉到之前的整个句子[=52] =].我要的是:
This is a sentence to catch.
Another sentence to catch.
我该怎么办?
编辑:
感谢 tân 的回复,效果很好(我不确定是否理解所有内容,但我会考虑一下...)。
如果您同意,让我们将问题扩展得更复杂一些。新玩具示例:
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.
- 测试 1bis:我测试了
.+(?![\w\s\n-=]+).+
但没有发现任何东西 :(
仅供参考,我在 python 上用 Parsimonious 实现了一些东西。
您可以尝试将此模式与您的编程语言一起使用:.+(?![\w\s\n-]+).+
这是一个使用 javascript 语言的示例(在您的主要语言中,您不需要更改模式):
var input =
`This is a sentence to catch.
Title that should not be caught
-------------------------------
Another sentence to catch.`;
console.log(input.match(/.+(?![\w\s\n-]+).+/g))
如果您正在使用 linux(特别是 Ubuntu),您可以试试这个:
This test was done on Ubuntu, I haven't tested it on other distributions
line_match=$(grep -nrE '^(------)+.*$' my_file | grep -oE '[0-9]+') && line_to_delect="$(($line_match-1))" && sed ${line_to_delect},${line_match}d my_file
如果您想匹配示例数据中的单行,一个选项是确保您匹配的第一行不以 --- 或 === 开头。
匹配第一行后,断言字符串的结尾 $
并使用另一个否定前瞻断言该行之后的行也不以 --- 或 ===[=21= 开头]
^(?!(?:---|===)).+$(?!\r?\n(?:---|===))
^
字符串开头
(?!
负向前瞻,断言直接右边的不是
(?:---|===)
匹配 ---
或 ===
)
关闭前瞻
.+$
匹配除换行符之外的任何字符 1+ 次并断言字符串结束
(?!\r?\n(?:---|===))
另一个前瞻性作为第一个前置换行符
这是一个看起来像重组文本的小文本。
This is a sentence to catch.
Title that should not be caught
-------------------------------
Another sentence to catch.
我想要一个正则表达式来捕获不是 header 的两行,并留下 是 的句子作为标题。
- 测试 #1:如何离开
---
行。我已经完成/^(?!(---))[^\n]+/gm
。它将行留在 header 下。它给了我:
This is a sentence to catch.
Title that should not be caught
Another sentence to catch.
- 测试 #2:如何在 header (
Title that should not be caught
) 行上方也留下句子?我试过/^(?!(---))[^\n]+(?!\n---)/gm
结果是:
This is a sentence to catch.
Title that should not be caught
Another sentence to catch.
问题是它没有捕捉到之前的字母 \n---
而我想要的是没有捕捉到之前的整个句子[=52] =].我要的是:
This is a sentence to catch.
Another sentence to catch.
我该怎么办?
编辑:
感谢 tân 的回复,效果很好(我不确定是否理解所有内容,但我会考虑一下...)。
如果您同意,让我们将问题扩展得更复杂一些。新玩具示例:
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.
- 测试 1bis:我测试了
.+(?![\w\s\n-=]+).+
但没有发现任何东西 :(
仅供参考,我在 python 上用 Parsimonious 实现了一些东西。
您可以尝试将此模式与您的编程语言一起使用:.+(?![\w\s\n-]+).+
这是一个使用 javascript 语言的示例(在您的主要语言中,您不需要更改模式):
var input =
`This is a sentence to catch.
Title that should not be caught
-------------------------------
Another sentence to catch.`;
console.log(input.match(/.+(?![\w\s\n-]+).+/g))
如果您正在使用 linux(特别是 Ubuntu),您可以试试这个:
This test was done on Ubuntu, I haven't tested it on other distributions
line_match=$(grep -nrE '^(------)+.*$' my_file | grep -oE '[0-9]+') && line_to_delect="$(($line_match-1))" && sed ${line_to_delect},${line_match}d my_file
如果您想匹配示例数据中的单行,一个选项是确保您匹配的第一行不以 --- 或 === 开头。
匹配第一行后,断言字符串的结尾 $
并使用另一个否定前瞻断言该行之后的行也不以 --- 或 ===[=21= 开头]
^(?!(?:---|===)).+$(?!\r?\n(?:---|===))
^
字符串开头(?!
负向前瞻,断言直接右边的不是(?:---|===)
匹配---
或===
)
关闭前瞻.+$
匹配除换行符之外的任何字符 1+ 次并断言字符串结束(?!\r?\n(?:---|===))
另一个前瞻性作为第一个前置换行符