[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!
[/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.