skip to Main Content

I am trying to search and replace in VS code thousands of instances, etc.

Sample String:

[[123|123]] and [[4567|4567]], also [[89|89]].

Finding [[.*|(.*)]] and replacing with [[$1]]. It finds [[123|123]] and [[4567|4567]], also [[89|89]]turns the whole string into [[123]].. The values inside the brackets could be any length of characters, but the pipe does separate them.

I tried anchoring start and end with ^[[.*|(.*)]]$, but then nothing matched.

Wanted result:

[[123]] and [[456]], also [[789]].

2

Answers


  1. Chosen as BEST ANSWER

    I found the answer, but I don't know why it works. I was just continuing to play and found that this works for the search:

    [[.*?|(.*?)]]
    

  2. If you want to find anything in between the brackets ([[ ]]), you need to filter for any character, which is not ] instead of any character (.).
    If not it will select the whole string.

    [^t]* = is every character except t

    [^]]* = is every character except ]

    So try [[[^]]*]] as regex

    Even if this does not solve your problem fully, it hopefully will give you the idea.

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