Reputation: 563
I'm attempting to modify some strings in a json in Notepad++ by adding a new line with some alternate text. For example, the input for one part of the document would be
"type": "apples_oranges_one",
"attribute": "bananas",
"type": "tomatoes_beans_celery",
"attribute": "bannanas",
and afterwards I would like the result to be
"type: "apples_oranges_one",
"type_alt": "strawberries_oranges_two",
"attribute": "bannanas",
"type": "tomatoes_beans_celery",
"attribute": "bannanas",
etc.
I have used Regex to accomplish this by using find/replace to find
"type":"apples_oranges_one",
and replace it with the string
"type": "apples_oranges_one", \n "type_alt": "strawberries_oranges_two",
Is there a way to do this and just append to "type": "apples_oranges_one", without having to replace the whole string? I know it seems like a small difference but it would make what I'm doing much more efficient.
Upvotes: 0
Views: 1136
Reputation: 1185
Use a capture group and refer to it in the replace.
Find:
("type": "apples_oranges_one",)
Replace:
\1\n "type_alt": "strawberries_oranges_two",
In Find, the ()'s denote a capture group. Everything captured by the sub-expression within the ()'s will be "captured" so that you can refer to it later.
In Replace, you refer to the captured group by using \1.
If you had multiple capture groups, each group would be numbered according to the ordering of that group's (
relative to all other capture groups' (
s (parenthesis that you don't want to match literally).
This means that for the regex ((a)(b))
, \1
would be ab
, \2
would be a
, and \3
would be b
.
Upvotes: 2
Reputation: 6549
Yes, it's possible.
With Regex expression you can use capturing groups ()
to match/capture a group and then in the replace part you can use \1, \2, \3 etc. to get the matched group back.
Find:
("type": "apples_oranges_one",) ("attribute": "bananas",)
Replace:
\1\n"type_alt": "strawberries_oranges_two",\n\2
Upvotes: 1