如何使用记事本++使用正则表达式附加某些字符串

How to append certain strings with Regex using notepad++

我试图通过添加一个带有一些替代文本的新行来修改 Notepad++ 中 json 中的一些字符串。例如,文档的一部分的输入为

"type": "apples_oranges_one",  
"attribute": "bananas",

"type": "tomatoes_beans_celery",  
"attribute": "bannanas",

然后我希望结果是

"type: "apples_oranges_one",  
"type_alt": "strawberries_oranges_two",  
"attribute": "bannanas",  

"type": "tomatoes_beans_celery",  
"attribute": "bannanas",  

等等

我使用正则表达式通过使用 find/replace 找到

来完成这个
"type":"apples_oranges_one",  

并将其替换为字符串

"type": "apples_oranges_one",  \n "type_alt": "strawberries_oranges_two",

有没有办法做到这一点,只需附加到 "type": "apples_oranges_one",而不必替换整个字符串?我知道这似乎是一个很小的区别,但它会使我做的事情更有效率。

使用捕获组并在替换中引用它。

查找:

("type": "apples_oranges_one",)

替换:

\n "type_alt": "strawberries_oranges_two",

在 Find 中,() 表示捕获组。 () 中的子表达式捕获的所有内容都将是 "captured",以便您稍后可以参考。

在替换中,您使用 \1 引用捕获的组。

如果您有多个捕获组,每个组将根据该组的 ( 相对于所有其他捕获组的 ( 的顺序进行编号(您 不想想按字面意思匹配)。

这意味着对于正则表达式 ((a)(b))</code> 将是 <code>ab</code> 将是 <code>a,而 </code> 将是成为 <code>b.

是的,有可能。

使用 Regex 表达式,您可以使用捕获组 () 到 match/capture 一个组,然后在替换部分您可以使用 \1、\2、\3 等来获取匹配的组返回。

查找:

("type": "apples_oranges_one",) ("attribute": "bananas",)

替换:

\n"type_alt": "strawberries_oranges_two",\n