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

Rakka Rage
  • 15,941
  • 8
  • 33
  • 45

3 Answers3

8

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

Stephane Janicaud
  • 3,531
  • 1
  • 12
  • 18
1

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

Mark
  • 143,421
  • 24
  • 428
  • 436
0

You can use the extension Regex Text Generator

  • select the numbers with multi cursors
  • execute command: Generate text based on Regular Expression (regex)
  • for original regex use: (.*)
  • for generator regex use: {{=N[1]-100}}
rioV8
  • 24,506
  • 3
  • 32
  • 49