2

I am struggling with regular expressions and how to use them in snippets in VSCode and I could really use some help (I am a beginner in that area). I have two regexp:

  • the first one is here to remove the extension from a file path: (.*)\.[^.]+$
  • the second one would output all the occurrences of a backlash in the file path: /(\\)/g

I would like to group them into one regex to use in a user code snippet (html) in VSCode.

From the following input: C:\folder0\myhtml.html I would like to get the following output from the code snippet using transformations: C:/folder0/myhtml (backlashes are replaced with forward ones and the extension is removed).

I know how to write snippets that do it independently:

${TM_FILEPATH/(.*)\\..+$/$1/} would produce C:\folder0\myhtml

${TM_FILEPATH/(\\\\)/\\//g} would produce C:/folder0/myhtml.html

TM_FILEPATH being C:\folder0\myhtml.html in my example. But I cannot combine them.

I have first tried to combine the regex in https://regex101.com/ like this: (\\)(.*)\.[^.]+$ but the result is not what I expect.

Mark
  • 143,421
  • 24
  • 428
  • 436
Benoit00
  • 45
  • 4
  • What have you tried so far? This is JS? Seems like a `|` should work for it... one expression has delimiters and modifier other does not, perhaps being used in different functions? – user3783243 May 23 '22 at 12:26
  • *"...so that I can then do things on those groups independently"*. Does this mean you are going to split the filepath **excluding** the extension? I mean, why would you want to capture the backslashes otherwise? – JvdV May 23 '22 at 12:28
  • you should really just ask what information you need to grab from a given string as an example. Your partial regex are pretty weak and are useless. For sure you can easily split the extension.. but it's not clear what you need those individual slashes for. – Diego D May 23 '22 at 12:53
  • Please use the `Save & Share` option on Regex101. That will make the question easier – user3783243 May 23 '22 at 14:11
  • Thanks for the edit. Makes things clear. Possibly use `\\([^\\.]*)(?:\..*$)?` and replace with `/$1` – JvdV May 23 '22 at 14:20
  • 1
    Hi @JvdV, thanks a lot for your suggestion. I was stuck with my idea of replacing somehow the \ with / but your idea is way better. I struggled a little bit with the additional backslashes I had to add in vscode but it works as expected. – Benoit00 May 24 '22 at 08:37

2 Answers2

1

Use

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

See regex proof.

EXPLANATION

NODE                     EXPLANATION
--------------------------------------------------------------------------------
  \\                       '\'
--------------------------------------------------------------------------------
  (                        group and capture to \1:
--------------------------------------------------------------------------------
    [^\\.]*                  any character except: '\\', '.' (0 or
                             more times (matching the most amount
                             possible))
--------------------------------------------------------------------------------
  )                        end of \1
--------------------------------------------------------------------------------
  (?:                      group, but do not capture (optional
                           (matching the most amount possible)):
--------------------------------------------------------------------------------
    \.                       '.'
--------------------------------------------------------------------------------
    .*                       any character except \n (0 or more times
                             (matching the most amount possible))
--------------------------------------------------------------------------------
  )?                       end of grouping
Ryszard Czech
  • 18,032
  • 4
  • 24
  • 37
1

In your case you can make it much simpler by using other variables (see snippet variables documentation).

"${TM_DIRECTORY/(\\\\)/\\//g}/$TM_FILENAME_BASE"

TM_DIRECTORY gets the full path up to the fileName. C:\folder0 in your example.
$TM_FILENAME_BASE gets the fileName without the extension. myhtml in your example.

So all you really need to do is swap those backslashes with forward slashes with the transform: ${TM_DIRECTORY/(\\\\)/\\//g} and concatenate the parts.

Mark
  • 143,421
  • 24
  • 428
  • 436