简体   繁体   English

正则表达式在 Notepad++ 中 Git 中的 Reverted Commit 中使用分隔符 `<<<<<<< HEAD` 和 `=======` 删除行

[英]Regex Delete Lines with delimiter `<<<<<<< HEAD` and `=======` in Reverted Commit in Git in Notepad++

Using a bad regex I accidentally deleted many lines I shouldn't have.使用错误的正则表达式,我不小心删除了许多我不应该删除的行。 After reverting the changes (a feature of Git Version Control), I have markdown files that look like this now:还原更改后(Git 版本控制的一个功能),我有 markdown 文件现在看起来像这样:

<<<<<<< HEAD
There was a sentence here:
There was a third line here.
=======
There was a sentence here:
There was a second line here.
There was a third line here.
There were any number of lines here.
>>>>>>> parent of <commit ID> (<commit msg>)

My request is to use <<<<<<< HEAD and ======= as delimiters and delete all what's between the delimiters, including the delimiters as well.我的要求是使用<<<<<<< HEAD=======作为分隔符并删除分隔符之间的所有内容,包括分隔符。 I would delete the >>>>>>> parent of <commit ID> (<commit msg>) bits separately afterwards.之后我将分别删除>>>>>>> parent of <commit ID> (<commit msg>)

My regex ( .* ) to match multiple lines between the delimiters was unsuccessful.我的正则表达式( .* )匹配分隔符之间的多行不成功。 I am using [{[(*"'!->1-9a-zA-ZÀ-ŰØ-űø-ÿ]+ instead of simple w+ to cater for any line-opening character/word I might want to be using. I have soft line breaks (two spaces) after each sentence, if that is important to you. (If you can match all what's between the delimiters, it might not even matter.)我正在使用[{[(*"'!->1-9a-zA-ZÀ-ŰØ-űø-ÿ]+而不是简单的w+来满足我可能想要使用的任何换行符/单词。我如果这对您很重要,请在每个句子后使用软换行符(两个空格)。(如果您可以匹配分隔符之间的所有内容,则可能无关紧要。)

Expected result:预期结果:

There was a sentence here:
There was a second line here.
There was a third line here.
There were any number of lines here.
>>>>>>> parent of <commit ID> (<commit msg>)

As I said, I would deal with >>>>>>> parent of <commit ID> (<commit msg>) afterwards.正如我所说,之后我将处理>>>>>>> parent of <commit ID> (<commit msg>)
Also, it goes without saying that it is not always two lines between delimiters.此外,不言而喻,定界符之间并不总是两条线。 Varying number of lines causes my issue.不同的行数会导致我的问题。

Instead of using a non greedy match, you can use a negative lookahead matching lines in between that do not consist only of ======= which is more perfomant:除了使用非贪婪匹配之外,您还可以在它们之间使用负前瞻匹配行,这些匹配行不仅包含=======更高效:

^<<<<<<< HEAD(?:\R(?!=======$).*)*+\R=======$

Explanation解释

  • ^ Start of string ^字符串开头
  • <<<<<<< HEAD Match literally <<<<<<< HEAD匹配字面意思
  • (?: Non capture group (?:非捕获组
    • \R Match any unicode newline \R匹配任何 unicode 换行符
    • (?!=======$) Negative lookahead, assert that the line is not ======= (?!=======$)负前瞻,断言该行不是=======
    • .* Match the whole line .*匹配整行
  • )*+ Close the non capture group and optionally repeat it using a possessive quantifier )*+关闭非捕获组并可选地使用所有格量词重复它
  • \R Match any unicode newline \R匹配任何 unicode 换行符
  • ======= Match literally =======字面匹配
  • $ End of string $字符串结尾

Regex demo正则表达式演示

在此处输入图像描述

After perusing an answer from Wiktor here在仔细阅读 Wiktor 答案后

I managed to find a match:我设法找到了一个匹配:

<<<<<<<[\d\D]*?=======

The ? ? is important to find more than one match within a document.在文档中找到多个匹配项很重要。 Otherwise you are liable to delete more stuff again.否则,您可能会再次删除更多内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM