java中的ruby gsub函数,replaceAll也许?

use*_*683 3 ruby java regex string replace

我一直试图翻译这个

    funcFormat = funcFormat.gsub(/sqrt\((.*)\)/,'Math.sqrt(\1)')
Run Code Online (Sandbox Code Playgroud)

在java中

    funcFormat = funcFormat.replaceAll("sqrt((.*))","Math.sqrt($1)"); 
Run Code Online (Sandbox Code Playgroud)

或者有什么方法可以将数学格式化为文本?例如:

2x ^ 2sqrt(x ^ 3/2)到2xpow2sqrt(xpow3/2)

谢谢你,顺便说一句,我是这个网站的新手.

Kep*_*pil 5

您可以使用以下表达式:

funcFormat = funcFormat.replaceAll("sqrt\\(([^)]*)\\)", "Math.sqrt($1)");
Run Code Online (Sandbox Code Playgroud)

虽然看起来你不需要这里的正则表达式.一个简单的

funcFormat = funcFormat.replace("sqrt", "Math.sqrt");
Run Code Online (Sandbox Code Playgroud)

似乎也适合你的情况.