如何在Notepad ++中使用正则表达式保留一部分字符串的同时替换字符串 [英] How to replace a string while while keeping a part of it using regex in Notepad++
问题描述
我有多个记录的文件:例如:
I have file with multiple records: for example:
"datetime": "2018-10-10"
"datetime": "2018-10-11"
"datetime": "2019-01-11"
"datetime": "2018-02-15"
我想替换这个字符串,这样我就可以保留日期值,所以是这样的:
I would like to replace this string such that i can retain the date values, so something of this sort:
"datetime": date("2018-10-10")
"datetime": date("2018-10-11")
"datetime": date("2019-01-11")
"datetime": date("2018-02-15")
我可以使用 "datetime": ".*"
来查找上述记录,但我坚持以上述格式替换它们感谢帮助
I am able to use "datetime": ".*"
to find the above records, but am stuck with replacing them in the above format
help is appreciated
推荐答案
您可以使用
查找内容:("datetime": )(".*")
替换为:\1date\(\2\)
详情
("datetime": )
- 第 1 组(替换模式中的\1
):文字"datetime":
子字符串(".*")
- 第 2 组(替换模式中的\2
):"
,除换行符,尽可能多,然后是"
(请注意,如果您的内容混合在一起,在这里使用非贪婪模式更安全,".*?"
)
("datetime": )
- Group 1 (\1
in the replacement pattern): a literal"datetime":
substring(".*")
- Group 2 (\2
in the replacement pattern):"
, any 0+ chars other than line break chars, as many as possible, and then a"
(note that in case your contents are mixed, it is much safer to use a non-greedy pattern here,".*?"
)
请注意,替换模式中的 (
和 )
必须被转义,因为 Notepad++ 正则表达式替换模式是 Boost 条件替换模式,括号在那里是特殊的".
Note that (
and )
inside the replacement pattern must be escaped as Notepad++ regex replacement patterns are Boost conditional replacement patterns and parentheses are "special" there.
这篇关于如何在Notepad ++中使用正则表达式保留一部分字符串的同时替换字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!