Regex which changes first letter of filename to lowercase in sublime text 3 snippet

I'd like to create following react snippet for stateless component.

import React from 'react';
const fileName= (props) => {


}

export default fileName;

so far I have following

<snippet>
 <content><![CDATA[
import React from 'react';
const ${1:${TM_FILENAME/(.+)..+..+/$1/}} = (props) => {


}

export default ${1:${TM_FILENAME/(.+)..+..+/$1/}};
]]></content>
 <!-- Optional: Set a tabTrigger to define how to trigger the snippet -->
 <!-- <tabTrigger>hello</tabTrigger> -->
 <tabTrigger>less</tabTrigger>
 <!-- Optional: Set a scope to limit where the snippet will trigger -->
 <!-- <scope>source.python</scope> -->
</snippet>

which outputs

import React from 'react';
const FileName= (props) => {


}

export default FileName;

How do I make first letter of the filename lowercase?

728x90

1 Answers Regex which changes first letter of filename to lowercase in sublime text 3 snippet

According to the SublimeText snippet documentation, the regex flavor here is Boost and it supports case changing operators in the replacement patterns.

You may use the following fix:

${1:${TM_FILENAME/(.+)..+..+/\l$1/}}
                             ^^

The \l operator turns the first char that stands to the right of the operator to lowercase.

Other options:

\l  Causes the next character to be outputted, to be output in lower case.
\u  Causes the next character to be outputted, to be output in upper case.
\L  Causes all subsequent characters to be output in lower case, until a \E is found.
\U  Causes all subsequent characters to be output in upper case, until a \E is found.
\E  Terminates a \L or \U sequence.

Note that your pattern does not look fine since it captures any 1 or more chars ((.+)) up to the last four (..+..+ matches that last 4 chars on a line due to the first greedy pattern). It might not do what you need.

If you plan to capture any text up to the last dot if present use

${1:${TM_FILENAME/^(.*?)(\.[^.]*)?$/\l$1/}}

where

  • ^ - start of string
  • (.*?) - Group 1 ($1): any 0+ chars other than line break chars, as few as possible
  • (\.[^.]*)? - Group 2 (optional): a . and then any 0+ chars other than .
  • $ - end of string.

4 months ago