Vscode: regex sub match math

Created on 10 Feb 2016  路  12Comments  路  Source: microsoft/vscode

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

It works in vim regex:

%s@Test (3[0-9]*)@\='Test '.(submatch(1)-100)@

Thanks.

editor-find feature-request

Most helpful comment

why not integrate a eval operator like ${some javascript}. Let me give an example:

input 99 bottles of beer on the wall, 99 bottles of beer. Take one down and pass it around, 98 bottles of beer on the wall.
search (\d+)
replace with ${$1 - 1}
result 98 bottles of beer on the wall, 98 bottles of beer. Take one down and pass it around, 97 bottles of beer on the wall.
search (\w+)
replace with ${$1.split('').reverse().join('')}
result 89 selttob fo reeb no eht llaw, 89 selttob fo reeb. Ekat eno nwod dna ssap ti dnuora, 79 selttob fo reeb no eht llaw.

I think that would be very useful.

All 12 comments

Right now the find/replace simply replaces. It doesn't do math ops, but that is a nice idea.

here is a working vim command, was missing slashes
%s@{fileID: \([1-9][0-9]*\)@\='{fileID: '.(submatch(1)-4594)@

the vim one seems to work with the parenthesis
maybe instead of $1 we could use %1 or something for numbers
idk

thanks

why not integrate a eval operator like ${some javascript}. Let me give an example:

input 99 bottles of beer on the wall, 99 bottles of beer. Take one down and pass it around, 98 bottles of beer on the wall.
search (\d+)
replace with ${$1 - 1}
result 98 bottles of beer on the wall, 98 bottles of beer. Take one down and pass it around, 97 bottles of beer on the wall.
search (\w+)
replace with ${$1.split('').reverse().join('')}
result 89 selttob fo reeb no eht llaw, 89 selttob fo reeb. Ekat eno nwod dna ssap ti dnuora, 79 selttob fo reeb no eht llaw.

I think that would be very useful.

I tried

%s@Test (3[0-9]*)@='Test '.(submatch(1)-100)@

But it does not work. It gives me the error:

E486: Pattern not found: Test (3[0-9]*)

When I change the pattern to %s@Test 3[0-9]*@\='Test '.(submatch(1)-100)@
Test 300
Test 301
Test 302

It finds the pattern but the result is:

Test -100
Test -100
Test -100

I am on MacOSX. What am I doing wrong please?

is this feature has any process?

The working command is from vim not vscode.

+1

+1

+1

+1

There has been a response to @rakkarage's StackOverflow question by now: this can be achieved by the Super Replace VS Code extension:

image

Was this page helpful?
0 / 5 - 0 ratings

Related issues

biij5698 picture biij5698  路  3Comments

vsccarl picture vsccarl  路  3Comments

trstringer picture trstringer  路  3Comments

curtw picture curtw  路  3Comments

borekb picture borekb  路  3Comments