496

I'm looking to do search replace with regular expressions in Sublime Text 2. The documentation on this is rather anemic. Specifically, I want to do a replace on groups, so something like converting this text:

Hello my name is bob

And this search term:

Find what: my name is (\w)+

Replace with: my name used to be $(1)

The search term works just fine but I can't figure out a way to actually do a replace using the regexp group.

6 Answers 6

659

Usually a back-reference is either $1 or \1 (backslash one) for the first capture group (the first match of a pattern in parentheses), and indeed Sublime supports both syntaxes. So try:

my name used to be \1

or

my name used to be $1

Also note that your original capture pattern:

my name is (\w)+

is incorrect and will only capture the final letter of the name rather than the whole name. You should use the following pattern to capture all of the letters of the name:

my name is (\w+)
9
  • 11
    No, $0 is not used as a back-reference in a regular expression (because \0 equals the null character, and \0ddd is used as an escape for a character with code point ddd). However, $0 does seem to be used in Sublime2 as an "exit mark" according to this page (though I'm having trouble finding enough examples to work out exactly how an exit mark is used in Sublime2).
    – Bobulous
    Apr 3, 2013 at 20:41
  • 4
    The exit mark $0 is used in Sublime Text 2/3 to place the cursor on exit from a snippet expansion. More on snippets.
    – jrhorn424
    Sep 30, 2013 at 6:45
  • 3
    $0 still works for backreferencing the entire expression in Sublime Text 2
    – sturrockad
    Jun 15, 2015 at 9:02
  • 6
    $0 is the entire regex match; $1 is the first capture group.
    – Oletha
    Sep 27, 2016 at 12:42
  • 1
    $0 worked for me in Sublime 3 to get the whole matched expression. I had a capturing group around the whole regex and a nested capturing group in the middle, and $1 was referencing the internal capturing group, which is strange because I thought capturing groups were ordered by opening parentheses. Hope this helps someone! Mar 8, 2017 at 17:40
110

By the way, in the question above:

For:

Hello, my name is bob

Find part:

my name is (\w)+

With replace part:

my name used to be \1

Would return:

Hello, my name used to be b

Change find part to:

my name is (\w+)

And replace will be what you expect:

Hello, my name used to be bob

While (\w)+ will match "bob", it is not the grouping you want for replacement.

0
78

Important: Use the ( ) parentheses in your search string

While the previous answer is correct there is an important thing to emphasize! All the matched segments in your search string that you want to use in your replacement string must be enclosed by ( ) parentheses, otherwise these matched segments won't be accessible to defined variables such as $1, $2 or \1, \2 etc.

For example we want to replace 'em' with 'px' but preserve the digit values:

    margin: 10em;  /* Expected: margin: 10px */
    margin: 2em;   /* Expected: margin: 2px */
  • Replacement string: margin: $1px or margin: \1px
  • Search string (CORRECT): margin: ([0-9]*)em // with parentheses
  • Search string (INCORRECT): margin: [0-9]*em

CORRECT CASE EXAMPLE: Using margin: ([0-9]*)em search string (with parentheses). Enclose the desired matched segment (e.g. $1 or \1) by ( ) parentheses as following:

  • Find: margin: ([0-9]*)em (with parentheses)
  • Replace to: margin: $1px or margin: \1px
  • Result:
    margin: 10px;
    margin: 2px;

INCORRECT CASE EXAMPLE: Using margin: [0-9]*em search string (without parentheses). The following regex pattern will match the desired lines but matched segments will not be available in replaced string as variables such as $1 or \1:

  • Find: margin: [0-9]*em (without parentheses)
  • Replace to: margin: $1px or margin: \1px
  • Result:
    margin: px; /* `$1` is undefined */
    margin: px; /* `$1` is undefined */
2
  • 1
    Thank you, this answer really points out a interesting fact that resolved my problem.
    – Enkk
    Mar 29, 2016 at 11:21
  • Thanks for pointing it out. With (), I can do the replace with \1 in Sublime 3.
    – Jeffrey
    Jun 5, 2020 at 20:07
33

Note that if you use more than 9 capture groups you have to use the syntax ${10}.

$10 or \10 or \{10} will not work.

2
  • Thanks! This was helpful for me to replace and add a number to the end of the captured group, e.g. to append a 1 to the second capture group you need to use braces like ${2}1; $21 refers to the 21st capture group, which doesn't exist.
    – Eric
    Mar 3, 2020 at 22:05
  • I was trying to replace $1 to $1 and 0 and but not $10. Your answer solved this problem. Now I used ${1}0 and I added 0 . Thanks..
    – BARIS KURT
    Apr 9, 2022 at 19:32
3

Looking at Sublime Text Unofficial Documentation's article on Search and Replace, it looks like +(.+) is the capture group you might want... but I personally used (.*) and it worked well. To REPLACE in the way you are saying, you might like this conversation in the forums, specifically this post which says to simply use $1 to use the first captured group.

And since pictures are better than words...

Before: before find/replace

After: after find/replace

0

I had similar problem:

Many strigs with this pattern like in Don Jos6 went (correct was Don Jo went)

I used this tool: https://coding.tools/regex-replace

Regular Expression: ([a-z])6

Replace To: $1é

Click on the "Regex Replace" Button. Result:

Don Jo went

It also works in Google Sheets.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.