I need the regular expression for multiplying the digits in the filename by 2 and subtracting 1. for eg: filename12.pdf should become filename23.pdf filename225.pdf should become filename449.pdf
Please help me with the regular expression.
I need the regular expression for multiplying the digits in the filename by 2 and subtracting 1. for eg: filename12.pdf should become filename23.pdf filename225.pdf should become filename449.pdf
Please help me with the regular expression.
Here is a way to do it in Perl:
output:
or in php :
A regex generally cannot do calculations, but it can help you capture the number. You can use a callback of replace. Here's C#, for example:
Helper method (we could have used a lambda, but it's less pretty):
Regex replace:
An important note here is that the string may represent a too large integer (so it won't parse). Also,
i*2-1
may overflow, resulting in a negative number. You may use achecked
block, or use aBigInteger
(with .Net 4).