Sublime Forum

Replace a patter preserving numbers


I want to find a pattern ['r1__access1__pic.210;

where 210 goes all the way up to 710, sequentially. I want to replace it with :

so just adding '] after the numbers and preserve the numbers



You can do this with a regex find/replace operation. To do that:

  1. Open the Replace panel (Find > Replace or the associated key)
  2. Ensure that regex is turned on (first button in the panel; the icon varies by theme but in the Default theme it looks like .*)
  3. In the Find: field, enter (r1__access1__pic\.\d{3})
  4. In the Replace: field, enter \1']
  5. Press the Replace All button

This finds the literal text r1__access1__pic, followed by a literal . character (that character is special in the regex, so \. tells the regex to not treat it specially), followed by exactly 3 digits. The whole regex is wrapped in () character to capture it for use in the replacement.

This expression will find all of the text that you outlined above (although it doesn’t care what the numbers that follow are, only that there are 3 digits) and for each match the text matched (which includes the numbers that are different on each line) is captured.

The replacement text includes \1 to indicate “the text of the first capture”, and then the rest is what to insert after it.

If you make sure that Highlight Matches is turned on in the panel (it’s the button to the left of the Replace field) then everything that matches the expression will be outlined so you can verify it’s doing what you want.

In this case since the expression starts with ( nothing will match until you enter the closing ) character, so you may want to type those both first and then fill in the middle so you can verify.

On the flip side if there are really 500 matches, the highlighting could potentially cause some slowdown. If you notice that, you can turn the option off for the duration of the find.

1 Like


Thank you so much! Worked perfectly!

1 Like