Sublime Forum

Select "any text" when replacing (beginner question)

#1

Hi,

Is there a way to search (and replace) any string? I want to delete various prices throughout a document, but they’re all different number - all they have in common is that they are preceded by a currency symbol (£) and followed by a line break (\n).

I’ll try to explain:
I have a giant JSON file with lots of entries for products. They show various features available for each product, which will then be shown on a website. Example:

"Product 4": { "details": "STANDARD \n first feature \n second feature \n OPTIONS AT EXTRA COST \n first option - £200 \n second option - £150 \n" }

This would then look like this:

Product 4: STANDARD first feature second feature OPTIONS AT EXTRA COST first option - £200 second option - £150

However, we have decided we don’t want the prices to be shown for those optional features. I could go through the entire sheet, deleting every mention of a price, but that would be too time consuming…

Instead, is there a way to search and replace? Like, could I search for:

 - £*** \n

… and replace it with just:

\n

… where the asterisks stand for “any symbol”?

I hope this makes sense. I’m sure it’s a feature of SublimeText, but I don’t know how to word it, so I haven’t been able to find it on the forum.

Thanks to anyone who can help me. And sorry for asking what is arguably a bit of a dumb question :smile:

0 Likes

#2

Hi, it is easy to handle these kind of tasks with multiple cursors:

  1. Select first ‘£’
  2. Find -> Quick find all
  3. Move cursory one char to right
  4. CMD-D (Expand selection to word)

All prices are selected now and you can delete them (and the surrounding chars), just as you would delete a single selection.

0 Likes

#3

[quote=“planet”]Hi, it is easy to handle these kind of tasks with multiple cursors:

  1. Select first ‘£’
  2. Find -> Quick find all
  3. Move cursory one char to right
  4. CMD-D (Expand selection to word)

All prices are selected now and you can delete them (and the surrounding chars), just as you would delete a single selection.[/quote]

Wow. Thank you so much! :smiley:

I had no idea this multiple-cursor thing was even an option! What an elegant solution, thanks again.

0 Likes

#4

The other option would be similar to your “- £*** \n” approach: using a regular expression.

  1. Ctrl+F
  2. Alt+R (to enable regular expression search, or the left-most button)
  3. Insert “£\d+”
  4. Hit “Find all”
  5. Do what @planet mentioned and treat them like a single selection
0 Likes

#5

To top off the above answers, a loose rule of thumb is that if the amount of chars you want to process is the same for each cursor and its easily identified, the multicursor approach as suggested by planet is usually sufficient. It should be in your use case.

However, when the characters you want to select vary from one instance to the next (eg. varying amounts of digits and you want to add ‘.00’ to the end), regular expressions are a more powerful tool for making multiple selections. Sublime follows the Perl RE syntax (google it) which has a very rich set of operators. You often don’t need to find & replace, just use “find all” to select everything and - for the above example - hit right-arrow to move each cursor to the end of its selection and type .00

:smile:

0 Likes

#6

Or, even better, multiple cursors with the SelectUntil plugin.

0 Likes

#7

The amount of chars per cursor doesn’t have to be the same if there’s a consistent boundary as you can use (shift+)option+right to select/jump to where you need all the cursors to be.

0 Likes

#8

Big thanks again to everyone who helped me in this thread!

Not only was my problem solved quickly and elegantly, I also have enough information to start learning more about SublimeText. :sunglasses:

0 Likes