I'm trying to use String.replaceAll
but I'm getting an error:
Here is my code:
public static String signSimplify (String str) {
String strr = str.replaceAll("--", "+");
String strr2 = strr.replaceAll("-+", "-");
String strr3 = strr2.replaceAll("+-", "-");
return strr3;
}
And, when executed, I get the following error:
Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '+' near index 0
+-
^
at java.util.regex.Pattern.error(Unknown Source)
at java.util.regex.Pattern.sequence(Unknown Source)
at java.util.regex.Pattern.expr(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.util.regex.Pattern.<init>(Unknown Source)
at java.util.regex.Pattern.compile(Unknown Source)
at java.lang.String.replaceAll(Unknown Source)
at fr.genius.main.Main.signSimplify(Main.java:108)
at fr.genius.main.Main.calcTrinome(Main.java:88)
at fr.genius.main.Main.main(Main.java:69)`
I don't understand why I can't use a replaceAll("+-", "-") but I can when + and - are inversed.
If you don't want or need regular expressions, just use replace()
instead of replaceAll()
and it will work like you expected.