Home Download Buy Blog Forum Support

Regex snippet question

Regex snippet question

Postby handycam on Fri Feb 24, 2012 4:03 pm

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:

Code: Select all
<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 <a id="foo-bar"/>Foo Bar
Mac OS 10.9.3
ST 3 (3059)
handycam
 
Posts: 271
Joined: Tue Jan 03, 2012 2:42 pm

Re: Regex snippet question

Postby kd7swh on Mon Feb 27, 2012 4:31 am

I also need this functionality, so I'll bump your post. :?:

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"
Mac OS 10.7.3
ST2 2181
kd7swh
 
Posts: 5
Joined: Mon May 09, 2011 1:13 am

Re: Regex snippet question

Postby handycam on Mon Feb 27, 2012 11:34 am

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

http://sublimetext.info/docs/en/extensibility/snippets.html

But I was wondering if anyone knows why this example is not working.
Mac OS 10.9.3
ST 3 (3059)
handycam
 
Posts: 271
Joined: Tue Jan 03, 2012 2:42 pm

Re: Regex snippet question

Postby nick. on Mon Feb 27, 2012 4:11 pm

This will get you lowercase:
Code: Select all
<snippet>
<content><![CDATA[
<a id="${SELECTION/(.)/\L\1\E/g}"/>$SELECTION
]]></content>
</snippet>


And this will get you dashes:
Code: Select all
<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:
Code: Select all
<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.
nick.
 
Posts: 266
Joined: Wed Jan 18, 2012 3:45 am

Re: Regex snippet question

Postby agibsonsw on Mon Feb 27, 2012 8:54 pm

Code: Select all
<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:

Code: Select all
<a id="${SELECTION/([a-zA-Z]+)(?:(\s+?)|\b)/?1:\L$1(?2:-)\E/g}"/>$SELECTION
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Regex snippet question

Postby nick. on Wed Feb 29, 2012 4:46 pm

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!
nick.
 
Posts: 266
Joined: Wed Jan 18, 2012 3:45 am

Re: Regex snippet question

Postby agibsonsw on Wed Feb 29, 2012 5:22 pm

Hello nick.

Code: Select all
<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'".

Andy.

PS I didn't know there was any such thing as a 'nested conditional' either - I just tried it :lol:
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Regex snippet question

Postby handycam on Wed Feb 29, 2012 10:26 pm

agibsonsw wrote:Hello nick.

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



What's that \E switch?
Mac OS 10.9.3
ST 3 (3059)
handycam
 
Posts: 271
Joined: Tue Jan 03, 2012 2:42 pm

Re: Regex snippet question

Postby agibsonsw on Wed Feb 29, 2012 10:53 pm

@handycam

\L means "lowercase til \E", so \E means "end case-modification".
"I'm here to save your life. But if I'm going to do that, I'll need total uninanonynymity." Me Myself & Irene.
agibsonsw
 
Posts: 901
Joined: Fri Jan 27, 2012 9:11 pm

Re: Regex snippet question

Postby brunobatista on Mon Jul 16, 2012 9:54 pm

How get first letter?

${1/(\w)(\w+)/\L\1/g}

But get small error!
brunobatista
 
Posts: 16
Joined: Thu Apr 26, 2012 2:13 pm

Next

Return to General Discussion

Who is online

Users browsing this forum: Exabot [Bot], Google [Bot], MystickPhoenix and 72 guests