Search code examples
regexvisual-studio-codecode-snippets

Snippets VS Code Regex


I need your help, I am building a snippets, but I need to transform the path of the file which is this:

D:\Project\test\src\EnsLib\File\aaa\bbb

and I need it to be like this:

EnsLib\File\aaa\bbb

just leave me from "SRC" forward and replace the \ with points.

Example: D:\Project\test\src\EnsLib\File\aaa\bbb

Result: EnsLib.File.aaa.bbb

that always after the src folder is the starting point

my test regex are these:

"${TM_DIRECTORY/(.*\\\\{4})/$1/}",
"${TM_DIRECTORY/.*src\\\\(.*)\\\\(.*)$/.$2/}.${TM_FILENAME_BASE}",
// "${TM_DIRECTORY/.*\\\\(.*)\\\\(.*)$/$1.$2/}.${TM_FILENAME_BASE}",
// "${RELATIVE_FILEPATH/\\D{4}(\\W)\\..+$/$1/g}",
// "${TM_DIRECTORY/(.*src\\\\)//g}.${TM_FILENAME_BASE}",
// "${RELATIVE_FILEPATH/(\\D{3})\\W|(\\..+$)/$1.$2/g}",
// "${RELATIVE_FILEPATH/\\W/./g}",

Solution

  • It seems you want

    "${TM_DIRECTORY/^.*?\\\\src\\\\|(\\\\)/${1:+.}/g}"
    

    The regex is ^.*?\\src\\|(\\), it matches

    • ^ - start of string
    • .*? - any zero or more chars other than line break chars, as few as possible
    • \\src\\ - \src\ string
    • | - or
    • (\\) - Group 1 ($1): a \ char.

    If Group 1 matches, the replacement is a ., else, the replacement is an empty string, i.e. the text from the start of string till \src\ is simply removed.