[英]Visual Studio Code: How to automate a simple regex-find and replace?
I try to create a simple regex-find and replace task in Visual Studio Code.我尝试在 Visual Studio Code 中创建一个简单的正则表达式查找和替换任务。
Currently I copy from the AD some Users to a temporary file in Visual Studio code and remove the "CN=" at the beginning of the line and all the aditional informations after the first "," (regex: ,.*$).目前,我将一些用户从 AD 复制到 Visual Studio 代码中的临时文件,并删除行开头的“CN=”和第一个“,”之后的所有附加信息(正则表达式:,.*$)。 This works fine with Find&Replace in VSCode but I have manually to type it in every time I want to remove this.
这适用于 VSCode 中的 Find&Replace,但每次我想删除它时,我都必须手动输入它。
So the question is, is it possible to automate this kind of task?所以问题是,是否有可能自动化这种任务? I know there are some external tools ( https://code.visualstudio.com/docs/editor/tasks ) but I'm struggling to get it working...
我知道有一些外部工具( https://code.visualstudio.com/docs/editor/tasks ),但我正在努力让它工作......
Edit: Example requested (my regex is working, that's not the problem:/. I need an example how to automate this task... )编辑:请求示例(我的正则表达式正在工作,这不是问题:/。我需要一个示例如何自动执行此任务...)
EXAMPLE例子
CN=Test User,OU=Benutzer,OU=TEST1,OU=Vert,OU=ES1,OU=HEADQUARTERS,DC=esg,DC=corp
Expected Output预期产出
Test User
This extension does the job:此扩展程序可以完成以下工作:
https://marketplace.visualstudio.com/items?itemName=joekon.ssmacro#overview https://marketplace.visualstudio.com/items?itemName=joekon.ssmacro#overview
It seems like the regex adheres to:似乎正则表达式坚持:
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
Create a file regex.json :创建一个文件regex.json :
[{
"command": "ssmacro.replace",
"args": {
"info": "strip out EOL whitespace",
"find": "\\s+$",
"replace": "",
"all": true,
"reg": true,
"flag": "gm"
}
}]
"info"
is just a reminder, doesn't do anything. "info"
只是一个提醒,不做任何事情。
Set a shortcut in keybindings.json :在keybindings.json 中设置快捷方式:
"key": "ctrl+9",
"command": "ssmacro.macro", "args": {"path": "C:\\...\\regex.json"}
You can batch multiple commands together [{...},{...}]
which is useful for applying a whole set of regex operations in one go.您可以将多个命令一起批处理
[{...},{...}]
,这对于一次性应用一整套正则表达式操作非常有用。
As of today, it seems it's still not possible without an extension.截至今天,如果没有扩展,似乎仍然不可能。 Here are 2 other extensions than the one proposed in the accepted answer (both are also open-source):
除了已接受的答案中提出的扩展之外,这里还有另外 2 个扩展(两者都是开源的):
● Batch Replacer (but it doesn't work on the documents open in the editor : "you must have a folder open for editing and all files in it will be updated." * ) ● Batch Replacer (但它不适用于在编辑器中打开的文档:“您必须打开一个文件夹进行编辑,并且其中的所有文件都将被更新。” * )
● Replace Rules : you simply add some rules in your settings.json
(open the palette with F1
or ctrl+shift+p
and select Preferences: open settings (JSON)
). ● 替换规则:您只需在
settings.json
添加一些规则(使用F1
或ctrl+shift+p
打开调色板并选择Preferences: open settings (JSON)
)。
"replacerules.rules": {
"Remove trailing and leading whitespace": {
"find": "^\\s*(.*)\\s*$",
"replace": "$1"
},
"Remove blank lines": {
"find": "^\\n",
"replace": "",
"languages": [
"typescript"
]
}
}
扩展文件夹是: %USERPROFILE%\\.vscode\\extensions
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.