How to replace a string while while keeping a part of it using regex in Notepad++

五迷三道 提交于 2019-12-20 05:34:07

问题


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")

I am able to use "datetime": ".*" to find the above records, but am stuck with replacing them in the above format help is appreciated


回答1:


You may use

Find what: ("datetime": )(".*")
Replace with: \1date\(\2\)

Details

  • ("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, ".*?")

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.



来源:https://stackoverflow.com/questions/54788303/how-to-replace-a-string-while-while-keeping-a-part-of-it-using-regex-in-notepad

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!