0

I am doing simple regex search and replace with Eclipse Find/Replace (I assume behind the scene it uses Java regex) I am finding some pattern with some numbers in it and while replacing I want to multiply those numbers and use that as a replacement. How do I achieve this with just regex

example string: input30 
regex pattern: input(\d)\d{1,2}
regex replace: output\1*2 // I expect output6 but I get output3*2
Lorenz Meyer
  • 19,166
  • 22
  • 75
  • 121
nir
  • 3,743
  • 4
  • 39
  • 63
  • What do you want to do with the number that appears after string "input"? – shruti1810 May 15 '15 at 18:37
  • 5
    You can do that in Perl (as described [here](http://stackoverflow.com/questions/5245087/math-operations-in-regex)) and in some other pattern matching engines for scripted languages (see, e.g., [here](http://unix.stackexchange.com/questions/17020/arithmetic-operations-in-regex)), but AFAIK you can't do this in Java purely with regex. – Ted Hopp May 15 '15 at 18:44

1 Answers1

0

Using the eval modifier in Perl, it would have been possible with this regex:

#!/usr/bin/perl -w
$_ = 'input30';
s/(input)(\d)\d{1,2}/$1.($2*2)/e;
print $_;

Unfortunately, Eclipse's Find & Replace tool doesn't support this modifier.

zessx
  • 68,042
  • 28
  • 135
  • 158