如何在 Visual Studio 代码片段中使用正则表达式 'uppercase and replace'?
How to use Regex to 'uppercase and replace' in Visual Studio Code's snippets?
我想在 Visual Studio 代码 1.33.1 上创建一个使用文件名创建 C++ class 的片段。
首先,我想设置"include guard",重点是使用文件名,替换每个'.'。通过'_'并将其全部设置为大写(标准):
#ifndef FILE_CLASS_HPP //filename: File.class.hpp
VSC documentation 为文件名提供了一些变量,并提供了一些 Regex 以更改为全部大写并将一个字符替换为另一个字符。
重点是:我从来没有做到这两件事,因为我对正则表达式一无所知。
我尝试手动加入正则表达式,但从未奏效:
#ifndef ${TM_FILENAME/(.*)/${1:/upcase}/[\.-]/_/g}
预期结果:
#ifndef FILE_CLASS_HPP
实际结果:
#ifndef ${TM_FILENAME/(.*)//upcase/[\.-]/_/g}
这应该有效:
"Filename upcase": {
"prefix": "_uc",
"body": [
"#ifndef ${TM_FILENAME/([^\.]*)(\.)*/${1:/upcase}${2:+_}/g}"
],
"description": "Filename uppercase and underscore"
},
([^\.]*)(\.)* group1: all characters before a period
group2: the following period
用大写替换所有 group1 的:${1:/upcase}
用 _
替换所有组 2 '
${2:+_}
是条件替换,所以你只需要在group1大写的末尾加一个_
if下面还有一个group2.
在这种情况下,g
全局标志是必需的,以捕获 group1group2 的所有出现,而不仅仅是第一个。
我想在 Visual Studio 代码 1.33.1 上创建一个使用文件名创建 C++ class 的片段。
首先,我想设置"include guard",重点是使用文件名,替换每个'.'。通过'_'并将其全部设置为大写(标准):
#ifndef FILE_CLASS_HPP //filename: File.class.hpp
VSC documentation 为文件名提供了一些变量,并提供了一些 Regex 以更改为全部大写并将一个字符替换为另一个字符。
重点是:我从来没有做到这两件事,因为我对正则表达式一无所知。
我尝试手动加入正则表达式,但从未奏效:
#ifndef ${TM_FILENAME/(.*)/${1:/upcase}/[\.-]/_/g}
预期结果:
#ifndef FILE_CLASS_HPP
实际结果:
#ifndef ${TM_FILENAME/(.*)//upcase/[\.-]/_/g}
这应该有效:
"Filename upcase": {
"prefix": "_uc",
"body": [
"#ifndef ${TM_FILENAME/([^\.]*)(\.)*/${1:/upcase}${2:+_}/g}"
],
"description": "Filename uppercase and underscore"
},
([^\.]*)(\.)* group1: all characters before a period
group2: the following period
用大写替换所有 group1 的:${1:/upcase}
用 _
'
${2:+_}
是条件替换,所以你只需要在group1大写的末尾加一个_
if下面还有一个group2.
在这种情况下,g
全局标志是必需的,以捕获 group1group2 的所有出现,而不仅仅是第一个。