I have matched a certain pattern and additionally a number via
perl -pe 's/\(pattern\)(\d)/ ... /'
I know that I can access the pattern by putting $1 where the ... are and the number by using $2. How can I now repeat pattern $2 times where the ... are? To be more specific, I have an expression like:
perl -pe 'while(s/Power\(((?:(?!Power\().)+?),2\)/(($1)*($1))/){}' file.txt
And I want to generalize this to not only match the 2, which is hardcoded in there to repeat $1 twice, but to match any number n and repeat $1 n times. All of this should still be done in a oneliner.
So for example calling the script onto expressions like
Power(Power(x,3),2)
should return
(((x)*(x)*(x))*((x)*(x)*(x)))