0

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.

1
  • Please format properly. Commented May 1, 2015 at 10:14

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I attempted this, but for some reason it did not work :( the search box returned the following error Find: Can't find the text "("type":"apples_oranges_one",)" could the fact that the string I'm looking for has quotation marks be messing with the syntax somehow?
Regular expressions don't care about quotation marks. Are you sure the regex box was checked? EDIT: I realize my mistake. There is a typo in the "Find" field I provided. Need to add a space after the ":". Sorry. Will fix.
Found the problem! It was the comma that appeared to be breaking it. instead of finding ("type":"apples_oranges_one",) I set it to find ("type":"apples_oranges_one"), and that did the trick! Thank you so much for your advice
1

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

1 Comment

Thank you :) I had to put the comma outside the end bracket to get that to work, but that's exactly what I was after

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.