skip to Main Content

I am working in Visual Studio Code and I have a very large JSON file to which I would like to make some edits.
In all instances where I have a line that starts "category": I would like to find the first instance of > and remove it and everything that follows excluding the final comma.
Then as a separate query, I would like to do something similar. I would like to find the last instance of > and remove it and everything that precedes it

So for example for the first query

"category": "Level1 > Level2 > Level3",

or

"category": "Level1 > Level2",

Would be replaced with

"category": "Level1",
thus stripping out everything from the first onwards >

and for the second query

"subCategory": "Level1 > Level2 > Level3",

Would be replaced with

"subCategory": "Level3",
basically stripping out everything from the last > back to the "

"Level1", "Level2" etc could be anything

My best efforts so far have been to Find

"category": ?(.+)

which finds the lines OK, but provides me with nothing to use as a replacement. I should know this, but Regex does seem to take an age to sink in.

2

Answers


  1. Chosen as BEST ANSWER

    I found my answer after much trial and error and thanks to an unknown user who posted a partial solution here, then deleted it very soon after. I didn't take note of the user name, unfortunately.

    For the first query: find any line that starts with "category": and remove anything after and including the first > up to the trailing comma. It ended up like so:

    In Visual Studio Code's find/replace dialogue (Ctrl+H)

    Find: ("category":s*)("[^>]*[^>s])s*>.*(",)

    Replace: $1$2$3

    The second query: Find any line that starts with "subCategory": and remove everything else up to and including the last > I used:

    Find: ("subCategory":s*").*[$>]*>s(.*",)

    Replace: $1$2


  2. This seems to be a little simpler than your regex’s.

    For the first query, see regex101 demo:

    Find: (?<=bcategory":.*)s*>.*(?=",)
    Replace: with nothing

    For the second query, see regex101 demo:

    Find: (?<=bsubCategory":s*")(.*>s*)
    Replace: with nothing

    These will work in the Find in a file widget, but not for a search across multiple files – due to the lookbehind of a non-fixed length.

    If you want it to work in a search across files then use:

    Find: (bcategory":.*?)s*>.*(?=",)
    Replace: $1

    Find: (bsubCategory":s*")(.*>s*)
    Replace: $1

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search