Java;字符串替换(使用正则表达式)? [英] Java; String replace (using regular expressions)?
本文介绍了Java;字符串替换(使用正则表达式)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
作为学校项目的一部分,我需要替换以下形式的字符串:
As part of a project for school, I need to replace a string from the form:
5 * x^3 - 6 * x^1 + 1
类似于:
5x<sup>3</sup> - 6x<sup>1</sup> + 1
我相信可以使用正则表达式来完成此操作,但是我还不知道该怎么做.
I believe this can be done with regular expressions, but I don't know how to do it yet.
你能帮我一下吗?
P.S.实际的任务是实现一个多项式处理Java应用程序,我正在使用它来将polynomial.toString()从模型传递到视图,并且希望使用html标签以一种漂亮的方式显示它.
P.S. The actual assignment is to implement a Polynomial Processing Java application, and I'm using this to pass polynomial.toString() from the model to the view, and I want do display it using html tags in a pretty way.
推荐答案
str.replaceAll("\\^([0-9]+)", "<sup>$1</sup>");
这篇关于Java;字符串替换(使用正则表达式)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文