如何自动化一个简单的正则find并替换

问题描述 投票:0回答:3
(Regex:

,

)之后删除line和所有其他信息。在VS代码中查找和替换可以很好地工作,但是我每次要删除此内容都可以手动键入它。

问题是,是否可以自动执行此类任务?我知道有一些外部工具(

https://code.visualstudio.com/docs/editor/tasks
),但我正在努力使它正常工作。
example
(我的正则正则是工作,这不是问题。我需要一个有关如何自动化此任务的示例):

,.*$

指望输出

CN=Test User,OU=Benutzer,OU=TEST1,OU=Vert,OU=ES1,OU=HEADQUARTERS,DC=esg,DC=corp

thisextension完成工作:
SSMACRO

它似乎是遵守

javascript正则表达式

regex replace visual-studio-code vscode-tasks
3个回答
20
投票

创建文件regex.json

Test User

[{ "command": "ssmacro.replace", "args": { "info": "strip out EOL whitespace", "find": "\\s+$", "replace": "", "all": true, "reg": true, "flag": "gm" } }]

只是一个提醒,什么也没做。

Keybindings.json中设置快捷方式:

"info"
您可以将多个命令批量划分

"key": "ctrl+9", "command": "ssmacro.macro", "args": {"path": "C:\\...\\regex.json"}

,这对于一个GO应用一组正则操作非常有用。
    

今天,如果没有扩展,似乎仍然不可能。除了公认答案中提出的一个外,还有另外两个扩展名(两者都是开源): batch batch替换(但它在编辑器中打开的文档上不起作用:“您必须有一个文件夹以进行编辑,并且所有文件都将被更新。”

**


ReplaceRules

:您只需在

11
投票
中添加一些规则(用

settings.json

    F1
  • 打开调色板,然后选择

    ctrl+shift+p)。 Preferences: open settings (JSON)

  • 这是我写的一个扩展程序,它允许您将查找/替换在文件中或跨文件中的搜索作为命名命令和/或作为键键入:输入和转换。 使用OP的原始问题,将此设置(在

    "replacerules.rules": {
        "Remove trailing and leading whitespace": {
            "find": "^\\s*(.*)\\s*$",
            "replace": "$1"
        },
        "Remove blank lines": {
            "find": "^\\n",
            "replace": "",
            "languages": [
                "typescript"
            ]
        }
    }
    
    )中进行:
    settings.json
    您还可以将其做出此设置的跨文件搜索:
    "findInCurrentFile": {                // in settings.json
      "reduceUserEntry": {
        "title": "Reduce User to ...",    // will appear in the Command Palette
        "find": "CN=([^,]+).*",
        "replace": "$1",
        "isRegex": true,
        // "restrictFind": "selections",     // default is entire document
      }
    },
    

    是独立的钥匙键:
    "runInSearchPanel": {
      "reduceUserEntry": {
        "title": "Reduce User to ...",      // will appear in the Command Palette
        "find": "CN=([^,]+).*",
        "replace": "$1",
        "isRegex": true
        
        // "filesToInclude": "${fileDirname}"
        // "onlyOpenEditors": true
        // and more options
      }
    }
    
扩展名还可以运行多个发现/替代品 - 只需将它们放入数组中:

1
投票

{ "key": "alt+r", // whatever keybinding you want "command": "findInCurrentFile", // or runInSearchPanel "args": { "find": "CN=([^,]+).*", "replace": "$1", "isRegex": true and替换的同样,制作它们的数组。

	

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.