Sublime Forum

[SOLVED] Making changes to $file in build system

#1

Hi there,
I am trying to change within the build system the content of $file from:
C:\folder\file.py

to
/mnt/c/folder/file.py

for now I only have a really simple build system
{
“cmd”: [“parser.exe”, “$file”],
}

but for reasons I need $file to be in linux notation and add something to the path. How would I do that?
Thanks!

0 Likes

#2

You can just use regex replacements. This should work:

{
    "cmd": ["parser.exe", "${file/(?:^([A-Z]):)|(\\\\)/(?1:\\/mnt\\/\\l$1)(?2:\\/)/g}"],
}

Explanation: Be aware that every backslash \ must be escaped \\

  • ${file:/A/B/g}: in the variable file match A and replace it by B everywhere/globally g

Match: (?:^([A-Z]):)|(\\)

  • (?:^([A-Z]):)
    • (?:...) hold this together, but don’t create a group
    • ^ match the start of the string
    • ([A-Z]) match any uppercase character and store it in group 1
    • : match a colon
  • | or
  • (\\) mach a backslash \ and store it in group 2

Replace: (?1:\/mnt\/\l$1)(?2:\/)

  • (?1:\/mnt\/\l$1)
    • (?1:...) if group 1 is not empty
    • \/mnt\/ insert /mnt/
    • \l$1 and the content of the group 1 converted to lowercase
  • (?2:\/) if group 2 is not empty insert a slash /
3 Likes

#3

Hey,
thanks a lot for your answer. Somehow sublime is not interpreting the regex. I saw in the manual that I can use regex and played with it but somehow nothing happens to the variable. Also in the expression you wrote I got an error at the \l which would prevent the build system to be executed at all.
Any idea?

0 Likes

#4

Sorry I just tested the regex not the regex inside a build system. Since build systems are JSON every backslash must be escaped, i.e. \\ instead of \ everywhere.

I updated the regex

0 Likes

#5

awesome man! regex will always remain obscure to me. it works! thanks a lot.

0 Likes

#6

Don’t you have to replace the colon still? It’s inside a non-capturing group.

0 Likes

#7

It is not captured, but matched, so it will be replaced.

1 Like