1

I'm trying to write VSCode snippet to populate a remote repo path using the local Windows repo path as the basis but struggling a little with the syntax, can anybody help?

"MetaData Snippet": {
    "prefix": "META",
    "body": [
      "<#",
      "    .SYNOPSIS",
      "        ${1:Enter general synopsis of the type of script}",
      "",
      "    .DESCRIPTION",
      "        ${2:Short description of what the script will do}",
      "",
      "    .PARAMETER ${3:ParameterNameHere}",
      "        ${4:Description of the parameter}",
      "",
      "    .EXAMPLE",
      "        ${5:Example of how to use the code and also expected output}",
      "",
      "    .NOTES",
      "        VSTS:   ${7:$TM_FILEPATH/(.)(:\\)(.*)(\\TSO NH)//g}",
      "",
      "        | Author            | QC                | VSTS Story ID     | Release Date  |",
      "        -----------------------------------------------------------------------------",
      "        | ${8:Author Name}  | ${9:Name of reviewer} | ${10:Story Number} | $CURRENT_DATE/$CURRENT_MONTH/$CURRENT_YEAR_SHORT      |",
      "#>"
    ],
    "description": "MetaData Snippet"
  }

EXAMPLE PATH: c:\Users\USERNAME\git\Cloud and Automation\.vscode\test.ps1

Essentially I want to chop out c:\Users\USERNAME\git\ and the remainder I want to replace the \ characters with /.

I know my syntax is wrong but I'm not sure I understand regex well enough and can't figure it out :(

To summarize I want to turn:

c:\Users\USERNAME\git\Cloud and Automation\.vscode\test.ps1 

into:

Cloud and Automation/.vscode/test.ps1 
5
  • 1
    If you'd like help specifically with the regex, simply put in the example line you are starting with, and what you'd like it transformed to. Are you just trying to replace \ with / in a string? Commented Jun 5, 2018 at 13:14
  • This is what I'm going for... Current: c:\Users\USERNAME\git\Cloud and Automation\.vscode\test.ps1 Result: Cloud and Automation/.vscode/test.ps1 Commented Jun 5, 2018 at 13:37
  • looks good. I don't know VS but could whip up a 4 line solution in python fast, but I suspect somebody with VS skills is going to answer this shortly. If not, leave a comment and I can put my python answer in and you can see how the regex works. Commented Jun 5, 2018 at 13:47
  • Yeah I think that would be helpful, at least so that I can understand the logic and may be able to figure out from there. This is where I've gotten to with the regex expression: (^.*Cloud)|(\) This seems to match 2 groups (Group1: 'file path' & Group2: '\'), I'm just failing at the transform. Commented Jun 5, 2018 at 15:00
  • Cool, I'll put an answer in Commented Jun 5, 2018 at 15:06

2 Answers 2

0

Try this:

"${TM_DIRECTORY/.*\\\\(.*)\\\\(.*)$/$1\\/$2/}/${TM_FILENAME}"

and see escaping in vscode snippets. You do have a . in there for .vscode, so you might have to accommodate for that as well - just add an escaped period to the second capture group (\..*) or more backslashes there but you might not need that at all.

Sign up to request clarification or add additional context in comments.

2 Comments

This is pretty damn close! The only issue I found was that it looks like it's creating a capture group for every group of characters between a pair of backslashes and the transform puts it back without slashes. This works great until you navigate to a different folder path depth and use it. Do you know of a way to capture "Beginning <---> git\" then remove, capture remaining '\' and replace with '/'? I may be completely misunderstanding regex
It gets the last two directories from TM_DIRECTORY each in their own capture group so that the path separator can be not captured but replaced by /, and then adds the filename to the end. I thought that was what you wanted? And I don't understand your question in the comment: "Beginning....git\?
-1

Although this in python and not VS, you can see the regex in action. As per the OP's request in the comments here is the example in python.

import re

data = r"c:\Users\USERNAME\git\Cloud and Automation\.vscode\test.ps1"
path = re.sub(r".*git\\", "", data)   #strip up to git
fixed_path = re.sub(r"\\", "/", path) #replace backslashes with slashes
print(fixed_path)

Note that I'm escaping the \ that might be part of what you are running into. I'm opting to strip the string instead of trying to capture parts of it, sometimes it's just easier that way :)

Output:

Cloud and Automation/.vscode/test.ps1

Comments

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.