VSCode Regex Snippet

时间:2018-06-05 11:51:26

标签: regex visual-studio-code code-snippets

我正在尝试使用本地Windows repo路径作为基础来编写VSCode代码段以填充远程repo路径,但是对于语法有点挣扎,有人可以帮忙吗?

"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"
  }

示例路径:c:\Users\USERNAME\git\Cloud and Automation\.vscode\test.ps1

基本上我想要删除c:\Users\USERNAME\git\,其余的我要用\替换/个字符。

我知道我的语法错了,但我不确定我是否理解正则表达式并且无法理解:(

总结一下,我想转:

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

成:

Cloud and Automation/.vscode/test.ps1 

2 个答案:

答案 0 :(得分:0)

试试这个:

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

并查看escaping in vscode snippets。你的.里面有一个.vscode,所以你可能也需要适应它 - 只需将一个转义的句点添加到第二个捕获组(\..*)或更多的反斜杠那里,但是你可能根本不需要那样。

答案 1 :(得分:-1)

虽然这在python而不是VS中,你可以看到正则表达式在行动。根据OP在这里的评论中的请求是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)

请注意,我正在逃避\,这可能是您遇到的问题的一部分。我选择剥离字符串而不是尝试捕获它的一部分,有时它更容易这样:)

输出:

Cloud and Automation/.vscode/test.ps1