Home Download Buy Blog Forum Support

remove file extension in snippet

remove file extension in snippet

Postby pier on Fri Jun 15, 2012 4:55 am

So I'm trying to do this to output the file name without the extension.

Code: Select all
${TM_FILENAME/^(.+)(\.[^ .]+)?$/}


But it outputs nothing... I copied the regex from stack overflow, and it should work.

I've seen in the ST documentation that for substitutions I still need to put a string format after the regex...

http://docs.sublimetext.info/en/latest/ ... ppets.html

${var_name/regex/format_string/}



... but I really have no clue what to put there.

Any help is appreciated.

TIA
pier
 
Posts: 92
Joined: Fri Jun 15, 2012 2:47 am

Re: remove file extension in snippet

Postby brianheylin on Sat Jul 07, 2012 6:41 pm

Hey @pier

I took a look in the C++ snippets directory and found what you are looking for in the class snippet

Code: Select all
class ${1:${TM_FILENAME/(.+)\..+|.*/$1/:name}}


That should do what you want ;)

Brian
brianheylin
 
Posts: 5
Joined: Sat Jul 07, 2012 6:36 pm

Re: remove file extension in snippet

Postby pier on Sun Aug 05, 2012 5:07 pm

It works!!!

So how would that work if I just wanted to filename printed, without being a cursor tab?
pier
 
Posts: 92
Joined: Fri Jun 15, 2012 2:47 am

Re: remove file extension in snippet

Postby pier on Tue Nov 06, 2012 2:33 am

Just in case anyone stumbles upon this...

This is the code to write directly the filename without the extension in a snippet. Without being a cursor tab...

Code: Select all
${TM_FILENAME/(.+)\..+|.*/$1/:name}
pier
 
Posts: 92
Joined: Fri Jun 15, 2012 2:47 am

Re: remove file extension in snippet

Postby bizoo on Tue Nov 06, 2012 7:27 am

What's the meaning of :name in this code ?
bizoo
 
Posts: 877
Joined: Wed Dec 08, 2010 6:53 am
Location: Switzerland

Re: remove file extension in snippet

Postby pier on Tue Nov 06, 2012 8:33 pm

bizoo wrote:What's the meaning of :name in this code ?


Good question... I removed it and it still works.

I will never be able to understand all this regex black magic...
pier
 
Posts: 92
Joined: Fri Jun 15, 2012 2:47 am

Re: remove file extension in snippet

Postby pier on Sun Dec 16, 2012 8:06 pm

So after some studying (don't laugh at me regex masters) I deciphered and distilled the expression.

In case anyone stumbles upon this I will explain how this works for absolute ST + regex beginners like me.

This is how to write the file name without the extension in a snippet

Code: Select all
${TM_FILENAME/(.+)\..+/$1/}


As said in my first post ST expects this kind of format ${variable name/regex/string format}

Variable
TM_FILENAME is the variable that contains the whole file name (eg:myFile.cpp)

Regex
(.+)\..+ creates a pattern that matches the filename (eg:myFile) and excludes the extension:
  • (.+) capture a group of any characters in any number of repetitions.
    • () is for group capture
    • . means any character
    • + means one or more repetitions of "any character"
  • \..+ and do not capture everything after (and including) a point
    • \. is a match for the point character
    • . means any character
    • + means one or more repetitions of "any character"

String format
$1 I have no clue what this means, and it confused me as I thought it was something related with tab stops... It seems to mean "the match of the regex"...

Well there it is.
pier
 
Posts: 92
Joined: Fri Jun 15, 2012 2:47 am

Re: remove file extension in snippet

Postby agibsonsw on Sun Dec 16, 2012 9:02 pm

${TM_FILENAME/(.+)\..+/$1/}

${text_to_search/regex/replacement/modifiers}
The replacement can be a regex and/or text expression.
The modifiers are:
g - global search
i - case-insensitive
m - multi-line search.

(.+)\..+
This means (capture any one character or more) that is followed by a dot . and any one character or more. So,
(capture_this).discard_this
The captured text (a capture-group is identified with brackets) - is referred to as $1 in the regex. If there were more groups they would be $2, $3, with $0 referring to the whole matched expression. [Outside of a regex $1 would refer to the snippet tab-positioned content.]

I don't know about :name :| maybe it gives the snippet position 1 the alias 'name' so that it can be referred to later in the snippet as ${:name} ?? Curious if anyone knows..

The attached text file is supposed to describe STs variant of regular expressions. I think it actually describes a compatible variant, so is only 96.4% accurate, or maybe 95% :).
Attachments
RE.zip
(4.9 KiB) Downloaded 64 times
"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: remove file extension in snippet

Postby agibsonsw on Sun Dec 16, 2012 9:40 pm

There are online tools like this and this as well as applications that can be downloaded to test regular expressions. Neither of the links are specific to ST-regex, but they are 90% the same - so, sufficient for most purposes. [The first link has a nag-screen.]

Homework: study the following and report back :)
Code: Select all
/^([A-Za-z]([A-Za-z]|[0-9]|[+\-.])*:(\/\/((([A-Za-z]|[0-9]|[-._~])|%[0-9A-Fa-f][0-9A-Fa-f]|[!$&'()*+,;=]|:)*@)?(\[(((([0-9A-Fa-f]){1,4}:){6}((([0-9A-Fa-f]){1,4}:([0-9A-Fa-f]){1,4})|([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35]))|::(([0-9A-Fa-f]){1,4}:){5}((([0-9A-Fa-f]){1,4}:([0-9A-Fa-f]){1,4})|([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35]))|(([0-9A-Fa-f]){1,4})?::(([0-9A-Fa-f]){1,4}:){4}((([0-9A-Fa-f]){1,4}:([0-9A-Fa-f]){1,4})|([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35]))|((([0-9A-Fa-f]){1,4}:){0,1}([0-9A-Fa-f]){1,4})?::(([0-9A-Fa-f]){1,4}:){3}((([0-9A-Fa-f]){1,4}:([0-9A-Fa-f]){1,4})|([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35]))|((([0-9A-Fa-f]){1,4}:){0,2}([0-9A-Fa-f]){1,4})?::(([0-9A-Fa-f]){1,4}:){2}((([0-9A-Fa-f]){1,4}:([0-9A-Fa-f]){1,4})|([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35]))|((([0-9A-Fa-f]){1,4}:){0,3}([0-9A-Fa-f]){1,4})?::([0-9A-Fa-f]){1,4}:((([0-9A-Fa-f]){1,4}:([0-9A-Fa-f]){1,4})|([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35]))|((([0-9A-Fa-f]){1,4}:){0,4}([0-9A-Fa-f]){1,4})?::((([0-9A-Fa-f]){1,4}:([0-9A-Fa-f]){1,4})|([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35]))|((([0-9A-Fa-f]){1,4}:){0,5}([0-9A-Fa-f]){1,4})?::([0-9A-Fa-f]){1,4}|((([0-9A-Fa-f]){1,4}:){0,6}([0-9A-Fa-f]){1,4})?::)|v+\.(([A-Za-z]|[0-9]|[-._~])|[!$&'()*+,;=]|:)+)\]|([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])|(([A-Za-z]|[0-9]|[-._~])|%[0-9A-Fa-f][0-9A-Fa-f]|[!$&'()*+,;=])*)(:([0-9])*)?(\/((([A-Za-z]|[0-9]|[-._~])|%[0-9A-Fa-f][0-9A-Fa-f]|[!$&'()*+,;=]|[:@]))*)*|\/(((([A-Za-z]|[0-9]|[-._~])|%[0-9A-Fa-f][0-9A-Fa-f]|[!$&'()*+,;=]|[:@]))+(\/((([A-Za-z]|[0-9]|[-._~])|%[0-9A-Fa-f][0-9A-Fa-f]|[!$&'()*+,;=]|[:@]))*)*)?|((([A-Za-z]|[0-9]|[-._~])|%[0-9A-Fa-f][0-9A-Fa-f]|[!$&'()*+,;=]|[:@]))+(\/((([A-Za-z]|[0-9]|[-._~])|%[0-9A-Fa-f][0-9A-Fa-f]|[!$&'()*+,;=]|[:@]))*)*|)(\?((([A-Za-z]|[0-9]|[-._~])|%[0-9A-Fa-f][0-9A-Fa-f]|[!$&'()*+,;=]|[:@])|[\/?])*)?(#((([A-Za-z]|[0-9]|[-._~])|%[0-9A-Fa-f][0-9A-Fa-f]|[!$&'()*+,;=]|[:@])|[\/?])*)?|(\/\/((([A-Za-z]|[0-9]|[-._~])|%[0-9A-Fa-f][0-9A-Fa-f]|[!$&'()*+,;=]|:)*@)?(\[(((([0-9A-Fa-f]){1,4}:){6}((([0-9A-Fa-f]){1,4}:([0-9A-Fa-f]){1,4})|([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35]))|::(([0-9A-Fa-f]){1,4}:){5}((([0-9A-Fa-f]){1,4}:([0-9A-Fa-f]){1,4})|([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35]))|(([0-9A-Fa-f]){1,4})?::(([0-9A-Fa-f]){1,4}:){4}((([0-9A-Fa-f]){1,4}:([0-9A-Fa-f]){1,4})|([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35]))|((([0-9A-Fa-f]){1,4}:){0,1}([0-9A-Fa-f]){1,4})?::(([0-9A-Fa-f]){1,4}:){3}((([0-9A-Fa-f]){1,4}:([0-9A-Fa-f]){1,4})|([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35]))|((([0-9A-Fa-f]){1,4}:){0,2}([0-9A-Fa-f]){1,4})?::(([0-9A-Fa-f]){1,4}:){2}((([0-9A-Fa-f]){1,4}:([0-9A-Fa-f]){1,4})|([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35]))|((([0-9A-Fa-f]){1,4}:){0,3}([0-9A-Fa-f]){1,4})?::([0-9A-Fa-f]){1,4}:((([0-9A-Fa-f]){1,4}:([0-9A-Fa-f]){1,4})|([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35]))|((([0-9A-Fa-f]){1,4}:){0,4}([0-9A-Fa-f]){1,4})?::((([0-9A-Fa-f]){1,4}:([0-9A-Fa-f]){1,4})|([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35]))|((([0-9A-Fa-f]){1,4}:){0,5}([0-9A-Fa-f]){1,4})?::([0-9A-Fa-f]){1,4}|((([0-9A-Fa-f]){1,4}:){0,6}([0-9A-Fa-f]){1,4})?::)|v+\.(([A-Za-z]|[0-9]|[-._~])|[!$&'()*+,;=]|:)+)\]|([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])\.([0-9]|[\x31-\x39][0-9]|1([0-9]){2}|2[\x30-\x34][0-9]|25[\x30-\x35])|(([A-Za-z]|[0-9]|[-._~])|%[0-9A-Fa-f][0-9A-Fa-f]|[!$&'()*+,;=])*)(:([0-9])*)?(\/((([A-Za-z]|[0-9]|[-._~])|%[0-9A-Fa-f][0-9A-Fa-f]|[!$&'()*+,;=]|[:@]))*)*|\/(((([A-Za-z]|[0-9]|[-._~])|%[0-9A-Fa-f][0-9A-Fa-f]|[!$&'()*+,;=]|[:@]))+(\/((([A-Za-z]|[0-9]|[-._~])|%[0-9A-Fa-f][0-9A-Fa-f]|[!$&'()*+,;=]|[:@]))*)*)?|(([A-Za-z]|[0-9]|[-._~])|%[0-9A-Fa-f][0-9A-Fa-f]|[!$&'()*+,;=]|@)+(\/((([A-Za-z]|[0-9]|[-._~])|%[0-9A-Fa-f][0-9A-Fa-f]|[!$&'()*+,;=]|[:@]))*)*|)(\?((([A-Za-z]|[0-9]|[-._~])|%[0-9A-Fa-f][0-9A-Fa-f]|[!$&'()*+,;=]|[:@])|[\/?])*)?(#((([A-Za-z]|[0-9]|[-._~])|%[0-9A-Fa-f][0-9A-Fa-f]|[!$&'()*+,;=]|[:@])|[\/?])*)?)$/

BTW It's not mine!! and it's a URL validator.
"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: remove file extension in snippet

Postby pier on Mon Dec 17, 2012 12:32 am

Mother of god, that must be some alien technology.

Thanks again for the info Andy!
pier
 
Posts: 92
Joined: Fri Jun 15, 2012 2:47 am


Return to Technical Support

Who is online

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