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.

1 Answer 1

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 \.
Sign up to request clarification or add additional context in comments.

2 Comments

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 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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.