Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

vscode regex sub match evaluate instead of concatenate?

Test 300
Test 301
Test 302

I can use regex find to loop through these:

Test (3[0-9]*)

When I try replace with math it concatenates instead of evaluates?

Test $1-100

So, it becomes:

Test 300-100

Is it possible to evaluate instead of concatenate, so it becomes:

Test 200

Thanks.

like image 419
Rakka Rage Avatar asked Feb 09 '16 03:02

Rakka Rage


2 Answers

You can use the VS Code Super Replace extension to achieve this.

Find field is the regular expression

Replace is the replace expression. Sub match with $$index syntax will be resolved using the function in Processing function field

Here is an example of use that answers your question :

Super Replace VS Code demo

like image 102
Stephane Janicaud Avatar answered Sep 21 '22 13:09

Stephane Janicaud


There are more extensions that can do this now, including one I wrote Find and Transform.

With this keybinding:

{
  "key": "alt+m",                     // whatever keybinding you want
  "command": "findInCurrentFile",
  "args": {
    "find": "(?<=Test\\s)(3\\d\\d)",  // get 300+ in capture group 1
    "replace": "$${ return $1 - 100 }",      // subtract 100 from capture group 1
    "isRegex": true
  }
}

evaluate math on a capture group

like image 27
Mark Avatar answered Sep 17 '22 13:09

Mark