Search code examples
jsonnotepad++

If a line contains X, how do I replace the next line with Y?


I'm trying to take a file with a whole bunch of text in a JSON file, and mass-edit the thing with Find & Replace rather than going through it line-by-line and copy+pasting the changes I want to make.

So for example, if I have a list of fruits and I want to simplify what type of tree it's grouped under then I would take this:

  "Fruit And Veg\\Fruits\\Apples - fuji apple": {
    "group": "Fuji apple tree"
  },
  "Fruit And Veg\\Fruits\\Apples - granny smith": {
    "group": "GrannySmith tree"
  },
  "Fruit And Veg\\Fruits\\Pears - nashi": {
    "group": "Nashi tree"
  },
  "Fruit And Veg\\Fruits\\Pears - concorde": {
    "group": "concorde pear tree"
  },

and have the find-and-replace tool change it to this:

  "Fruit And Veg\\Fruits\\Apples - fuji apple": {
    "group": "Apple Tree"
  },
  "Fruit And Veg\\Fruits\\Apples - granny smith": {
    "group": "Apple Tree"
  },
  "Fruit And Veg\\Fruits\\Pears - nashi": {
    "group": "Pear Tree"
  },
  "Fruit And Veg\\Fruits\\Pears - concorde": {
    "group": "Pear Tree"
  },

I want to have it search for a string of text within line 1 (in this case, Apples or Pears), then replace the contents of line 2 (so the line matches the Apple Tree or Pear Tree group, as applicable), and make no changes to line 3, for each entry in the file. I understand I would need to run a Find & Replace for Apples and Pears separately, and that's okay, but I'm not sure how to make this work.

At the moment, I think the file has the text I'm using to search only as plurals - but if that turns out not to be the case, is there a way for me to specify the Find part of the Find&Replace to be looking for "\\Apple" (to include the previous characters) instead of searching for "Apple" or "Apples"?

I've tried looking at other questions here, to see if I can find an identical issue, but haven't had much luck with that - and while I've been trying to figure out how to use ^.* \r\n $1, etc. from examples given on those pages on Stack Overflow, I don't really know what I'm doing at all. Even with 'Search Mode - Regular Expression', and toggling on '. matches newline', most of the things I've tried so far have either resulted in "Find: Can't find the text" or "Find: Invalid regular expression".


Solution

    • Ctrl+H
    • Find what: Fruit And Veg.+?(?:(Apple)|(Pear))s?.+\R.+?"group": \K".+tree"
    • Replace with: (?1"Apple Tree")(?2"Pear Tree")
    • TICK Match case
    • TICK Wrap around
    • Search Mode Regular expression
    • UNTICK . matches newline
    • Replace all

    Explanation:

    Fruit And Veg   # literally
    .+?             # 1 or more any character, not greedy
    (?:             # non capture group
        (Apple)         # group 1, Apples
      |               # OR
        (Pear)          # group 2, Pears
    )               # end group
    s?              # optional s
    .+              # 1 or more any character
    \R              # any kind of linebreak
    .+?             # 1 or more any character, not greedy
    "group":        # literally
    \K              # forget all we have seen until this position
    ".+tree"        # 1 or more character and tree
    

    Replacement:

    (?1"Apple Tree")    # if group 1 exists, pur Apple Tree
    (?2"Pear Tree")     # if group 2 exists, put Pear Tree
    

    Screenshot (before):

    enter image description here

    Screenshot (after):

    enter image description here