Thursday, December 22, 2016

Sublime copying all text that match a pattern

I had a text file containing numbers in between text that I wanted to copy, move to a new file and format them.

Since the numbers were of of fixed length and at the same index I could have copied them using vertical select. The vertical select is tricky if the file is big and it wouldn't help if the pattern to looks for is different and can occur anywhere in the text.

I knew the first way was to write a regular expression for the pattern that I was looking for. I could highlight all the matches with my pattern. But now how do I copy it?

I couldn't beleive it could be as simple as clicking Find All. The Find All gives a cursor to all the match with text selected. Amazing!!!

So Ctrl+c will copy all of them. Bravo!

I put the data on another file where I have to format them. This was easy. I used replace and group capture.

Find :      (455\d\d\d\d\d\d)
Replace : ($1),

The above will put all the matches within ( ... ),

Again I would have used Find All and used the cursor to modify all the words and put the brackets around them.

Thanks to sublime!!

No comments: