Sublime Forum

Regex snippet question


I am trying to make a snippet that takes the selected text and puts it into a tag ID. I can do it with a single word, but if the selection contains multiple words, I was to replace the spaces in the selection with hyphens before inserting as the ID. This is not working:

<content><![CDATA[ <a id="$SELECTION/\s/-/g"/>$SELECTION ]]></content>

For that matter, how would I also transform the selection to lowercase? What I am looking for is to select something like “Foo Bar” and get Foo Bar


Chained (Piped?) Snippet REGEX substitutions?
Regex capture groups in snippets
Regex support -- doesn't ST2 use the Boost library?

I also need this functionality, so I’ll bump your post. :question:

I’m rusty with my regex but I’m guessing that it would be an easier fix in Perl or some other scripting language.
In Ruby, you could just use the downcase method. Foo-Bar.downcase would render “foo-bar”



Thanks. I know several ways I could probably accomplish it, but I was just trying to use the syntax here:

But I was wondering if anyone knows why this example is not working.



This will get you lowercase:

<snippet> <content><![CDATA[ <a id="${SELECTION/(.)/\L\1\E/g}"/>$SELECTION ]]></content> </snippet>

And this will get you dashes:

<snippet> <content><![CDATA[ <a id="${SELECTION/\s/-/g}"/>$SELECTION ]]></content> </snippet>
Unfortunately I don’t think it’s possible to do both at once. A nested regex doesn’t work, and it isn’t possible to assign variables.

This seems like the best solution:

<snippet> <content><![CDATA[ <a id="${1:${SELECTION/\s/-/g}}"/>$SELECTION ]]></content> </snippet>
That will leave the id section selected. Then just use CTRL+K, CTRL+L command to convert to lowercase.


<a id="${SELECTION/([a-zA-Z]+)(\s?)/?1:\L$1(?2:-)\E/g}"/>$SELECTION

seems to work, although I’m testing it with $TM_CURRENT_LINE rather than selection.

I think it needs to be slightly more complex:

<a id="${SELECTION/([a-zA-Z]+)(?:(\s+?)|\b)/?1:\L$1(?2:-)\E/g}"/>$SELECTION


Andrew, can you explain your second example a bit? Specifically the ?1: syntax; it’s as if you’re doing conditional substitution. I didn’t know that was possible, and I’d like to know more!



Hello nick.

<a id="${SELECTION/([a-zA-Z]+)(?:(\s+?)|\b)/?1:\L$1(?2:-)\E/g}"/>$SELECTION

([a-zA-Z]+) captures one or more letters into group 1, provided they are also followed by space(s) or a word-break.
(?:(\s+?)|\b) is a non-capturing group, but if there are one or more spaces these *are *captured into a group, 2.

The ? following \s+ is necessary to create a non-greedy capture, otherwise the ‘g’ modifier will possibly force a capture of the whole line in one go!

?1: is a conditional, which says ‘if anything was captured in group 1’, insert this…
(?2:-) is a nested-conditional, and it will be ignored if there was no capture for group 2.

(?2:-:Doh) would (two colons : ) extend the conditional to say: “if group 2 was captured, insert a hyphen, otherwise insert ‘Doh’”.


PS I didn’t know there was any such thing as a ‘nested conditional’ either - I just tried it :laughing:



[quote=“agibsonsw”]Hello nick.

<a id="${SELECTION/([a-zA-Z]+)(?:(\s+?)|\b)/?1:\L$1(?2:-)\E/g}"/>$SELECTION


What’s that \E switch?




\L means “lowercase til \E”, so \E means “end case-modification”.



How get first letter?


But get small error!



This was very helpful! I was looking for a way to automatically generate the cHeader file #ifndef / #ifdef stuff using the filename and starting with the info above I was able to end up with

#ifndef INCLUDED_${TM_FILENAME/([a-zA-Z]+)(?:(\.+?)|\b)/?1:\U$1(?2:_)\E/g}
#define INCLUDED_${TM_FILENAME/([a-zA-Z]+)(?:(\.+?)|\b)/?1:\U$1(?2:_)\E/g}


which, if the filename is myfile.h gives



Then I went crazy and created this snippet

$1 == package
$2 == class
$3 == constructor arguments
$10 == decide if purpose/example exists. backspace if you don't want it
$11 == purpose
$12 == decide if example exists. backspace if you don't want it.
$13 == example

$0 == end point == class parameters
// package $1

#ifndef INCLUDED_${1/([a-zA-Z]+)(?:(\.+?)|\b)/?1:\U$1(?2:_)\E/g}_${TM_FILENAME/([a-zA-Z]+)(?:(\.+?)|\b)/?1:\U$1(?2:_)\E/g}
#define INCLUDED_${1/([a-zA-Z]+)(?:(\.+?)|\b)/?1:\U$1(?2:_)\E/g}_${TM_FILENAME/([a-zA-Z]+)(?:(\.+?)|\b)/?1:\U$1(?2:_)\E/g}

#include <string>

namespace coderascal \{
namespace ${1:package} \{

// Class: ${2:${TM_FILENAME/([a-zA-Z]+)(?:(\.t?)|\b).h/?1:\u$1(?2:_T)/g}}
// Author: Dennis Gove (coderascal)
// Purpose: 
//  ${11:Don't forget to fill in your purpose for class $2}
// Example:
//  ${13:Examples are really (really) (super really) helpful. Come on. Do it.}}

class $2 \{
    $2(const $2& orig);


\} // end class

\} // namespace $1
\} // namespace coderascal

    <tabTrigger>newh</tabTrigger> -->