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: 76
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

Re: How to REPLACE (not find) using regex

Postby The Guv on Thu Oct 16, 2014 12:09 am

Thank you, thank you, thank you, thank you Pete340!!!!!!

After hours of searching (well ok, an hour of searching), your post has been the only one that has made sense on how to do a search and replace with regex. As a designer, most of the answers on stackoverflow etc. just doesn't make sense, but yours finally let me change tags while leaving content. Brilliant!

And for anyone else searching, the way to change/replace tags while leaving the content is:

Original: <span class="text-italics">my content here</span>

Find what: <span class="text-italics">(.*)</span>
Replace with:<em>$1</em>

Equals: <em>my content here</em>
The Guv
 
Posts: 2
Joined: Thu Oct 16, 2014 12:05 am

Re: How to REPLACE (not find) using regex

Postby pete340 on Thu Oct 16, 2014 12:33 am

The Guv wrote:Thank you, thank you, thank you, thank you Pete340!!!!!!


You're welcome.

After hours of searching (well ok, an hour of searching), your post has been the only one that has made sense on how to do a search and replace with regex. As a designer, most of the answers on stackoverflow etc. just doesn't make sense, but yours finally let me change tags while leaving content. Brilliant!

And for anyone else searching, the way to change/replace tags while leaving the content is:

Original: <span class="text-italics">my content here</span>

Find what: <span class="text-italics">(.*)</span>
Replace with:<em>$1</em>

Equals: <em>my content here</em>


Just a caution: this won't do what you want if you have a nested span. For example, it will turn

<span class="text-italics">something <span something-else>more stuff</span></span

into

<em>something <span something-else>more stuff</em></span
pete340
 
Posts: 76
Joined: Mon Oct 10, 2011 9:45 pm

Re: How to REPLACE (not find) using regex

Postby The Guv on Thu Oct 16, 2014 9:37 am

Just a caution: this won't do what you want if you have a nested span. For example, it will turn

<span class="text-italics">something <span something-else>more stuff</span></span

into

<em>something <span something-else>more stuff</em></span


Thanks, I've just found that with a few occurrences but it's saved me so much time I'm happy to keep an eye out while manually checking.

I have yet to find a good noob's guide on this but obviously worth learning.
The Guv
 
Posts: 2
Joined: Thu Oct 16, 2014 12:05 am


Return to Technical Support

Who is online

Users browsing this forum: Yahoo [Bot] and 19 guests