简体   繁体   English

在Notepad ++中使用正则表达式进行迭代?

[英]Iterate with regex in Notepad++?

I have a text file like this: 我有一个这样的文本文件:

(A) foo
(B) bar
(B) eggs
x spam

(A), (B) etc. are priority tags. (A),(B)等是优先级标签。 They get changed, after which the file is sorted so that lines with prio (A) are on top. 它们得到更改,然后对文件进行排序,以使带有prio(A)的行位于顶部。 I'm wondering if there is a one-line regex search & replace that will replace ONLY THE FIRST tag (A), the first (B) and so on, with CR/LF [tag], so that the file looks like this: 我想知道是否存在单行正则表达式搜索和替换,它将仅用CR / LF [tag]替换第一标记(A),第一标记(B)等,以便文件看起来像这样:

(A) foo

(B) bar
(B) eggs

x spam

It's easy to make a script that does this. 编写执行此操作的脚本很容易。 But a regex, callable as a macro in Notepad++ would be just perfect. 但是,在Notepad ++中可以作为宏调用的正则表达式将是完美的。 Any thoughts? 有什么想法吗?

Something like this should do it: 这样的事情应该做到:

Replace 更换

(\([^)]*\))(.*\r\n)(?!\1)

with

\1\2\r\n

Explanation: 说明:

[^)]* means zero or more characters that aren't ) . [^)]*表示零个或多个字符不属于)
\\([^)]*\\) - just gets something in brackets (you need to escape ( and ) with \\ ). \\([^)]*\\) -只是放在括号中(您需要使用\\进行转义() )。
Surrounded by () to store it in the first group (use \\1 to get it). ()包围以将其存储在第一组中(使用\\1进行获取)。
.*\\r\\n - anything followed by a new line. .*\\r\\n任何内容后跟换行。
Surrounded by () to store it in the second group (use \\2 to get it). ()包围以将其存储在第二组中(使用\\2进行获取)。
(?!\\1) - negative look-ahead, the same tag shouldn't be on the next line. (?!\\1) -前瞻性否定,下一个行不应使用相同的标签。

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

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