Java; Replace String (using regular expressions)?

As part of the project for school, I need to replace the line from the form:

5 * x^3 - 6 * x^1 + 1 

to something like:

 5x<sup>3</sup> - 6x<sup>1</sup> + 1 

I believe that this can be done with regular expressions, but I still don't know how to do it.

Can you lend me a hand?

PS The actual purpose is to implement an application for processing polynomial Java processing, and I use this to pass polyomial.toString () from the model to the view, and I want it to be displayed using the html tags beautifully.

+74
java regex
Mar 10 '09 at 20:46
source share
12 answers
 str.replaceAll("\\^([0-9]+)", "<sup>$1</sup>"); 
+115
Mar 10 '09 at 20:50
source share
 private String removeScript(String content) { Pattern p = Pattern.compile("<script[^>]*>(.*?)</script>", Pattern.DOTALL | Pattern.CASE_INSENSITIVE); return p.matcher(content).replaceAll(""); } 
+18
Nov 20 '13 at 13:06
source share
 String input = "hello I'm a java dev" + "no job experience needed" + "senior software engineer" + "java job available for senior software engineer"; String fixedInput = input.replaceAll("(java|job|senior)", "<b>$1</b>"); 
+10
Sep 08 '12 at 11:29
source share
 import java.util.regex.PatternSyntaxException; // (:?\d+) \* x\^(:?\d+) // // Options: ^ and $ match at line breaks // // Match the regular expression below and capture its match into backreference number 1 «(:?\d+)» // Match the character ":" literally «:?» // Between zero and one times, as many times as possible, giving back as needed (greedy) «?» // Match a single digit 0..9 «\d+» // Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» // Match the character " " literally « » // Match the character "*" literally «\*» // Match the characters " x" literally « x» // Match the character "^" literally «\^» // Match the regular expression below and capture its match into backreference number 2 «(:?\d+)» // Match the character ":" literally «:?» // Between zero and one times, as many times as possible, giving back as needed (greedy) «?» // Match a single digit 0..9 «\d+» // Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+» try { String resultString = subjectString.replaceAll("(?m)(:?\\d+) \\* x\\^(:?\\d+)", "$1x<sup>$2</sup>"); } catch (PatternSyntaxException ex) { // Syntax error in the regular expression } catch (IllegalArgumentException ex) { // Syntax error in the replacement text (unescaped $ signs?) } catch (IndexOutOfBoundsException ex) { // Non-existent backreference used the replacement text } 
+8
Mar 10 '09 at 20:54
source share

If this is for any general mathematical expression, and parentheses are acceptable, it will be very difficult (perhaps impossible) to do this with regular expressions.

If the only replacements are the ones you showed, it’s not so difficult to do. Align * first, then use the grab as shown in the figure so that Can Berk Güder processes ^ .

+3
Mar 10 '09 at 20:56
source share

What is your polynomial? If you "process" it, I will introduce some kind of tree of subexpressions generated at some point, and would think that it would be much easier to use this to generate your string than to re-analyze the raw expression with the regular expression.

Just throw another way of thinking. I'm not sure what else is going on in your application.

+3
Mar 10 '09 at 21:05
source share
 "5 * x^3 - 6 * x^1 + 1".replaceAll("\\W*\\*\\W*","").replaceAll("\\^(\\d+)","<sup>$1</sup>"); 

note that combining both substitutions in a single regex / substitution would be a bad choice, because more general expressions like x^3 - 6 * x .

+3
Jul 23 '16 at 21:52
source share
 class Replacement { public static void main(String args[]) { String Main = "5 * x^3 - 6 * x^1 + 1"; String replaced = Main.replaceAll("(?m)(:?\\d+) \\* x\\^(:?\\d+)", "$1x<sup>$2</sup>"); System.out.println(replaced); } } 
+1
Sep 16 '12 at 11:57
source share

You will want to look at the capture in regular expression to handle the wrapper 3 in ^ 3.

0
Mar 10 '09 at 20:50
source share

Try the following:

 String str = "5 * x^3 - 6 * x^1 + 1"; String replacedStr = str.replaceAll("\\^(\\d+)", "<sup>\$1</sup>"); 

Be sure to import java.util.regex.

0
Mar 10 '09 at 20:57
source share

Take a look at antlr4. This will allow you to make significant progress in creating a tree structure than just regular expressions.

https://github.com/antlr/grammars-v4/tree/master/calculator (calculator.g4 contains the desired grammar)

In a nutshell, you define a grammar for parsing an expression, using antlr to generate java code, and adding callbacks to handle evaluations when building a tree.

0
Apr 12 '16 at 14:16
source share

Try this, maybe not the best way. but it works

 String str = "5 * x^3 - 6 * x^1 + 1"; str = str.replaceAll("(?x)(\\d+)(\\s+?\\*?\\s+?)(\\w+?)(\\^+?)(\\d+?)", "$1$3<sup>$5</sup>"); System.out.println(str); 
-2
Feb 11 '16 at 18:30
source share



All Articles