-2

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.

marcospereira
  • 12,045
  • 3
  • 46
  • 52
SEVENTH
  • 13
  • 1
  • 4
    did you try to read the documentation of the `replaceAll` method? – njzk2 Oct 05 '16 at 19:04
  • 1
    It's a regex. `+` is a metachar for the regex, and REQUIRES something "Before" it for it to work. `-+` works, because the `+` is modifying the `-`, but `+-` DOESN'T, because there's nothing before `+` to be modified. – Marc B Oct 05 '16 at 19:06
  • If you're going to use StackOverflow to ask questions that you could easily use Google to find the answer for, then at least select an answer below as the correct answer. Tom N's answer is the best answer, so if you can't decide, then select his answer. – searchengine27 Oct 05 '16 at 21:57
  • I've searched on google for half an hour before getting here (maybe that i have searched the wrong keywords), and sorry I didn't knew that I had an answer to choose. – SEVENTH Oct 12 '16 at 13:38

3 Answers3

2

If you don't want or need regular expressions, just use replace() instead of replaceAll() and it will work like you expected.

Kayaman
  • 72,141
  • 5
  • 83
  • 121
  • Ok, but it will replace all +- ? Or juste one ? thank you for the tip ! – SEVENTH Oct 05 '16 at 19:12
  • It will do a normal replacement of one string with another. If you were looking to replace both `+` and `-`, then you'll need to fix your regexp in one of several possible ways. Although it looks like you *don't* need a regexp. – Kayaman Oct 05 '16 at 19:15
  • @SEVENTH Yes, it will replace all instances. – shmosel Oct 05 '16 at 19:16
  • Ok, I will use this instead, thank you very much to both of you :) – SEVENTH Oct 05 '16 at 19:18
1

replaceAll in Java treats the text to replace as a Regular Expression. You'll need to escape the +.

str.replaceAll("\\+-", "-")
shmosel
  • 49,289
  • 6
  • 73
  • 138
Tom N.
  • 96
  • 4
  • Ok , i've already seen that for html or php but I had forgot how to use it, thank you very much ! – SEVENTH Oct 05 '16 at 19:09
0

Use the replacement in below technique:

String url="abc+-abc";
System.out.println(url.replaceAll("\\+-", "-"));
Rishal
  • 1,480
  • 1
  • 11
  • 19