可以说我有以下表达式:
2^3+4
5*3 + log(9) + 32/4 - 2.1^5
44 - 22 + sin(30)*2.34 + 51.32^4 + 17*32/11
我想查找并捕获之前的'^'数字和之后的数字,然后将其替换为Math.pow(captured1,captured2); 因此字符串将类似于:
Math.pow(2,3)+4
5*3 + log(9) + 32/4 - Math.pow(2.1,5)
44 - 22 + sin(30)*2.34 + Math.pow(51.32,4) + 17*32/11
它必须与Java regex兼容。
我对此的想法:
找:
(.*\d(\.\d)?)\^(.*\d(\.\d)?)
更换:
Math.pow(\1,\2)
提前致谢。
String result = subject.replaceAll("(-?\\d+(?:\\.\\d+)?)\\^(-?\\d+(?:\\.\\d+)?)", "Math.pow($1,$2)");
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句