Sublime Forum

How to REPLACE (not find) using regex

#1

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[54]
Find: .\d*.
Replace: ^\w*

I want this: geography
I get this instead: ^\w*

How can I fix this? Thanks! :smile:

0 Likes

#2

[quote=“rufustank”]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[54]
Find: .\d*.
Replace: ^\w*

I want this: geography
I get this instead: ^\w*

How can I fix this? Thanks! :smile:[/quote]

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©d)e the first capture group is (b©d) and the second capture group is ©. 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.

3 Likes

#3

Thanks so much for this help! It took a while to figure out capture groups but I got it. Immensely helpful!

0 Likes

#4

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: my content here

Find what: (.*)
Replace with:$1

Equals: my content here

0 Likes

#5

You’re welcome.

[quote]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: my content here

Find what: (.*)
Replace with:$1

Equals: my content here[/quote]

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
0 Likes

#6

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

something more stuff</span

into

something more stuff</span[/quote]

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.

0 Likes

#7

Thanks @pete340 this post really helped me.

0 Likes

#8

Thank you so much for this answer. Long time user of Sublime Text and finally had a use case for this.
Created an account just to thank you. You have my utmost gratitude Sir.

0 Likes

#9

340/5000
Hi,
Sorry to raise the subject but I’m desperate.

I use the following regex: [[a-zA-Z]+([a-zA-Z]+[a-zA-Z0-9])*? ] To find all the variables of the type $array[ search], $array[test2] … to replace them with $array[‘search’], $array[‘test2’].
I can’t do the replacement using $1 or $&.

Someone would know how to do it?

Thanks and sorry for my rough English.

0 Likes

#10

How about find (\$array\[)([^\]]+)(\]) and replace with \1'\2'\3?

0 Likes

#11

I finally got to understand how this regex work.

0 Likes