Home Download Buy Blog Forum Support

How to REPLACE (not find) using regex

How to REPLACE (not find) using regex

Postby rufustank on Tue May 06, 2014 3:56 pm

I'm a bit of a noob with Sublime and I've spent hours looking for the answer but can't seem to find the solution.

I am using regex to find and replace. Regex seems to work for "finding" code/text but if I want to paste using regex, it just pastes the regex commands and not the results of the command. I can't seem to find anything to turn on or off this functionality but I see people talking about being able to do it.

For example:
Text: geography<a href="../Text/Glossary.html#geography"><span class="glossary">[54]</span></a>
Find: <span class="glossary">.\d*.</span>
Replace: ^\w*

I want this: <a href="../Text/Glossary.html#geography">geography</a>
I get this instead: <a href="../Text/Glossary.html#geography">^\w*</a>

How can I fix this? Thanks! :)
rufustank
 
Posts: 2
Joined: Tue May 06, 2014 3:47 pm

Re: How to REPLACE (not find) using regex

Postby pete340 on Tue May 06, 2014 11:14 pm

rufustank wrote:I'm a bit of a noob with Sublime and I've spent hours looking for the answer but can't seem to find the solution.

I am using regex to find and replace. Regex seems to work for "finding" code/text but if I want to paste using regex, it just pastes the regex commands and not the results of the command. I can't seem to find anything to turn on or off this functionality but I see people talking about being able to do it.

For example:
Text: geography<a href="../Text/Glossary.html#geography"><span class="glossary">[54]</span></a>
Find: <span class="glossary">.\d*.</span>
Replace: ^\w*

I want this: <a href="../Text/Glossary.html#geography">geography</a>
I get this instead: <a href="../Text/Glossary.html#geography">^\w*</a>

How can I fix this? Thanks! :)


A regular expression is a pattern for matching text; it's used when searching. As you've seen, it doesn't do much of anything useful as a replacement. In order to copy text that matches part of a regular expression you have to mark the part that you want to be able to copy. You do this with a capture group, marking the text with a pair of parentheses. For example, in the regular expression a(b*)c the capture group will refer to all of the b's that come between a and c in the text that was searched. Once you have a capture group you can insert its contents into the replacement text with $n, where n is the number of the capture group. So to insert the matched b's in this example into replacement text, use $1.

You can have more than one capture group in a regular expression. The parentheses nest, so in a(b(c)d)e the first capture group is (b(c)d) and the second capture group is (c). The groups are numbered in order of the occurrence of their left parentheses. Also, $& refers to the entire match, so you don't need to write parentheses around the entire regular expression to get the text that matched it; you can just use $&.

EDIT: replaced ECMA-script style back references with Perl style, which is what Sublime uses.
pete340
 
Posts: 65
Joined: Mon Oct 10, 2011 9:45 pm

Re: How to REPLACE (not find) using regex

Postby rufustank on Thu May 08, 2014 6:38 am

Thanks so much for this help! It took a while to figure out capture groups but I got it. Immensely helpful!
rufustank
 
Posts: 2
Joined: Tue May 06, 2014 3:47 pm


Return to Technical Support

Who is online

Users browsing this forum: Exabot [Bot], Yahoo [Bot] and 24 guests