As part of a project for school, I need to replace a string from the form:
5 * x^3 - 6 * x^1 + 1
to something like:
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.
Can you lend me a hand?
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.
The Regex. Replace(String, String, MatchEvaluator, RegexOptions) method is useful for replacing a regular expression match in if any of the following conditions is true: The replacement string cannot readily be specified by a regular expression replacement pattern.
Java String replaceAll() The Java String class replaceAll() method returns a string replacing all the sequence of characters matching regex and replacement string.
The replaceAll() method replaces each substring that matches the regex of the string with the specified text.
str.replaceAll("\\^([0-9]+)", "<sup>$1</sup>");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With