34

I'm making a simple program that will deal with equations from a String input of the equation When I run it, however, I get an exception because of trying to replace the " +" with a " +" so i can split the string at the spaces. How should I go about using

the string replaceAll method to replace these special characters? Below is my code

Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '+' near index 0 + ^

 public static void parse(String x){
       String z = "x^2+2=2x-1";

       String[] lrside =  z.split("=",4);
       System.out.println("Left side: " + lrside[0] + " / Right Side: " + lrside[1]);
       String rightside = lrside[0];
       String leftside = lrside[1];

       rightside.replaceAll("-", " -");
       rightside.replaceAll("+", " +");
       leftside.replaceAll("-", " -"); leftside.replaceAll("+", " +");
       List<String> rightt = Arrays.asList(rightside.split(" "));
       List<String> leftt = Arrays.asList(leftside.split(" "));

       System.out.println(leftt);
       System.out.println(rightt);
3
  • 9
    '+' is a meta char in a regex, use "\\+" in the regex will do.
    – zw324
    Commented Apr 25, 2013 at 14:36
  • People coming her from PySpark world: use '\\\\+' Commented Jun 15, 2023 at 21:55
  • @TrigonaMinima I've tried that but then I have a NullPointerEx when applied to this: trim(split(wbana0,'\\\*')[1]) as name
    – DiegoMG
    Commented Aug 14, 2023 at 16:13

4 Answers 4

58

replaceAll accepts a regular expression as its first argument.

+ is a special character which denotes a quantifier meaning one or more occurrences. Therefore it should be escaped to specify the literal character +:

rightside = rightside.replaceAll("\\+", " +");

(Strings are immutable so it is necessary to assign the variable to the result of replaceAll);

An alternative to this is to use a character class which removes the metacharacter status:

rightside = rightside.replaceAll("[+]", " +");

The simplest solution though would be to use the replace method which uses non-regex String literals:

rightside = rightside.replace("+", " +"); 
2
  • Thanks, worked perfectly. I also tried rightside.replace("+", " +") with success but i'm not sure why this worked.
    – apache
    Commented Apr 25, 2013 at 14:47
  • rightside.replace("+", " +") worked as String.replace does not use a regular expression in its target (1st) argument.....
    – Reimeus
    Commented Apr 28, 2015 at 18:49
2

I had similar problem with regex = "?". It happens for all special characters that have some meaning in a regex. So you need to have "\\" as a prefix to your regex.

rightside = rightside.replaceAll("\\+", " +");
1

String#replaceAll expects regex as input, and + is not proper pattern, \\+ would be pattern. rightside.replaceAll("\\+", " +");

-1

The reason behind this is - There are reserved characters for regex. So when you split them using the java split() method, You will have to use them with escape. FOr example you want to split by + or * or dot(.) then you will have to do it as split("\+") or split("\*") or split("\.") according to your need.

The reason behind my long explanation on regex is -

YOU MAY FACE IT in OTHER PLACES TOO.

For example the same issue will occur if you use replace or replaceAll methods of java Because they are also working based on regex.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.