Sublime Forum

Remove file extension in snippet

#1

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

${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…

docs.sublimetext.info/en/latest/ … ppets.html

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

Any help is appreciated.

TIA

0 Likes

#2

Hey @pier

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

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

That should do what you want :wink:

Brian

0 Likes

#3

It works!!!

So how would that work if I just wanted to filename printed, without being a cursor tab?

0 Likes

#4

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…

${TM_FILENAME/(.+)\..+|.*/$1/:name}
1 Like

#5

What’s the meaning of :name in this code ?

0 Likes

#6

Good question… I removed it and it still works.

I will never be able to understand all this regex black magic…

0 Likes

#7

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

${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:
[ul]
li capture a group of any characters in any number of repetitions.
[list]
[]b[/b] is for group capture [/li]
[li]. means any character[/li]
[li]+ means one or more repetitions of “any character”[/li][/ul] [/
:m]
[li] …+ and do not capture everything after (and including) a point
[ul]
[]. is a match for the point character[/li]
[li]. means any character[/li]
[li]+ means one or more repetitions of “any character”[/li][/ul][/
:m][/list:u]

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.

0 Likes

#8

${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 :neutral_face: 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% :smile:.
RE.zip (4.9 KB)

0 Likes

#9

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 :smile:

/^([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.

0 Likes

#10

Mother of god, that must be some alien technology.

Thanks again for the info Andy!

0 Likes