Sublime Forum

Regex to do replace with all elements inside a list?

#1

Hi,

I need to do lot of replacements for my json file.

I have the following text elements:

[“Text1”,“Text2”]

which I need to convert to:

“[‘Text1’,‘Text2’]”

I hope I can do this with regular experssion such that for the first I need to chnage […] to “[…]” and secondly replace all elements inside…

Thank you!

0 Likes

#2

It cannot done via a single regex unless there are always exactly two elements in the list.
I may copy those lists into a new buffer, do some simple text replacement and paste back.

0 Likes

#3

Ok, then I would do it manually for 1, 2,4 items etc, That is fine!

Could you post an example how ou would do it?

0 Likes

#4

Actually you would have to do it for 1,2,3,4,… items if you want to use regex.

That strictly depends on how the actually text structure is. Demostration is not much helpful without know the actual text structure in my opinion…

0 Likes

#5

Thank you. But I postet the structure…

This indeed look like what I want. Could you post the keyboard keys to that in your video?

I hope this works also for a dict type like json filew here above lists are the keys.

So:

{
[“Text1”,“Text2”]: “BLALA”
}

0 Likes

#6

The following steps are for vanilla ST without any plugin installed.

  • use any way to place carets in all lists
  • repeatedly press ctrl+shift+m until all lists are selected
  • ctrl+c to copy
  • ctrl+n to open a new tab and ctrl+v to paste
  • alt+f3 will select all occurrences of the current selected characters. This is what I used to replace all ' into ".
  • after making changes, copy-and-paste back to the original tab (you have to guarantee there are same amounts of selected regions when you do copy-and-paste.
0 Likes

#7

What you mean by that? What are carets ?

0 Likes

#8

cursors. as you can see in the video.

0 Likes

#9

I know how you feel jfcherng. :smile:

0 Likes

#10

Sorry but this is self-explanatory??

0 Likes

#11

It seems there a shortcuts to mark specific elemnts inside a list all at once and this also in the multi cursor mode. I dont know the first one so I can’t search for it. But any way “any way” is very unspecific. :wink:

0 Likes

#12

Without given EXACT text content, there is no way I can suggest how to select XXX.

For example, alt+f3 can select ALL occurrences. Nice!!
But what if there is unfortunately one occurrence I don’t want? BOOM.

There are lots of convenient commands that I don’t want to mention given that I don’t have enough information from the OP and I don’t want to waste my time to list and make a tutorial for them all.

0 Likes

#13

I don’t understand. How did you selected then the elements inside the lists of your video? I just wanted to know that…

My elements of the lists are strings within the double quotes " ". I thought I could you use this info like regex: “*”

But this seems not possible.

0 Likes

#14

But I suppose one can imagine that if someone states that these are strings… How would I know what specific Text this is? That are arbitrary sentences… with pattern “***”…

I really try to figure out your instructions! But I feel you instructions rely on having elemenst which begin with “Text”?

0 Likes

#15
  • using mouse: alt + hold/drag right click
  • using keyboard: ctrl+alt+↓/↑
0 Likes

#16

No. But if the content has extra characteristics, it may be simpler.

0 Likes

#17

Thank you for y<our effort. But I am really sorry. Now, I am completely off! I have no intuition ahnd the simple shortcut combination makes no sense if I do not know what to do with it…I cannot imagine how you use alt + hold/drag right click to select all elements within the list… Sorry… And then this is point 1?

Really sorry
:pensive::pensive:

0 Likes

#18
  • hold both alt and right click
  • and then drag
0 Likes