1

I am struggling with a regex that works fine on regex101: https://regex101.com/r/Hhj2l9/1, but not in a vscode snippet for html.

From the following string: C:\folder0\folder1\folder2\libtest\folder3\folder4\folder5

I get the following results on regex101: libtest/libfolder3/libfolder4/folder5, which is what I want.

In my snippet:

lib${TM_DIRECTORY/(?:.*lib)?([^\\\\.*]*)\\\\/$1\\//g}

The result below in the html:

lib${TM_DIRECTORY/(?:.*lib)?([^\.*]*)\/\//g}

Has anyone an idea on how to make it work in a vscode snippet? Thanks a lot in advance.

Benoit00
  • 45
  • 4

1 Answers1

1

You can use

"${TM_DIRECTORY/(?:.*[\\\\\\/]lib)?([^\\\\\\/]+)[\\\\\\/]/lib$1\\//g}"

Here,

  • (?:.*[\\\/]lib)? - matches an optional occurrence of any zero or more chars other than line break chars as many as possible, and then \ or / and lib
  • ([^\\\\\\/]+) - Group 1 ($1): one or more chars other than \ and /
  • [\\\\\\/] - a / or \.
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
  • Great thanks a lot it works! Do you have any idea why my first attempt did not work? (I am curious to know, I am a bit of a beginner in regex) – Benoit00 Jun 03 '22 at 14:37
  • @Benoit00 Your regex only added `lib` at the start of the text and the backslashes that match a literal backslash cannot appear immediately before the `/` regex delimiter char, so I put it into a character class, `[...]`. Also, note that `[^\\\\.*]` matches any char other than a ``\``, `.` and `*`. I think `.` and `*` are typos here. – Wiktor Stribiżew Jun 03 '22 at 15:03