Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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: ,.*$). This works fine with Find&Replace in VSCode but I have manually to type it in every time I want to remove this.

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...

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
like image 275
Oskar Avatar asked Apr 03 '18 13:04

Oskar


People also ask

How do you write regex in Visual Studio code search?

Vscode has a nice feature when using the search tool, it can search using regular expressions. You can click cmd+f (on a Mac, or ctrl+f on windows) to open the search tool, and then click cmd+option+r to enable regex search. Using this, you can find duplicate consecutive words easily in any document.

How do I change the regex code in Visual Studio?

How to enable VSCode regex replace. First, you need to press Ctrl + H on Windows and Linux, or ⌥⌘F on Mac to open up search and replace tool. In order to activate regex search and replace in VSCode, you have to click on the . * button near the input.

How do you replace all occurrences of a word in VSCode?

You can use the Find control in code or text windows, such as Output windows and Find Results windows, by selecting Edit > Find and Replace or pressing Ctrl+F.


2 Answers

This extension does the job:

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

Example

Create a file 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.

Set a shortcut in 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.

like image 76
Phil B Avatar answered Oct 18 '22 03:10

Phil B


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):

● 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."*)

● 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)).

"replacerules.rules": {
    "Remove trailing and leading whitespace": {
        "find": "^\\s*(.*)\\s*$",
        "replace": "$1"
    },
    "Remove blank lines": {
        "find": "^\\n",
        "replace": "",
        "languages": [
            "typescript"
        ]
    }
}
like image 7
MagTun Avatar answered Oct 18 '22 04:10

MagTun