Read Also: [Solved] java.lang.IndexOutOfBoundsException in Java
What is Meta-character?
According to Oracle docs, a metacharacter is a character with special meaning interpreted by a matcher.The difference between an ordinary character and a metacharacter is that an ordinary character in a regular expression matches itself whereas a metacharacter is a special character that affects the way a pattern is matched. For example, the punctuation mark . is a metacharacter that matches any single character.
The characters '*', '+', '?' are also some examples of metacharacter.
[Fixed] java.util.regex.PatternSyntaxException: dangling meta character '+' near index 0
As always, first, we will produce the java.util.regex.PatternSyntaxException before moving on to the solution.Example 1: Producing the exception by splitting a string containing one of the metacharacters
You will get this exception when you are trying to split a string containing one of the metacharacters using the Java String split() method as shown below in the example:
public class PatternSyntaxExceptionExample { public static void main(String args[]) { String str = "Alive+is+Awesome";String[] substrings = str.split("+");for(String string : substrings) { System.out.println(string); } } }
Output:
Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '+' near index 0
+
^
Explanation
The split() method accepts a regular expression pattern, we need to escape the metacharacters(such as +,*,?) to resolve the issue.
Solution
The escape characters should be used are "\\+", "\\*", "\\?" as shown below in the example:
public class PatternSyntaxExceptionExample { public static void main(String args[]) { String str = "Alive+is+Awesome";String[] substrings = str.split("\\+");for(String string : substrings) { System.out.println(string); } } }
Output:
Alive
is
Awesome
Example 2: Producing the exception by trying to replace a substring containing one of the metacharacters using the replaceAll() method
You will get this exception when you are trying to replace a substring containing one of the metacharacters using the replaceAll() method as shown below in the example. Java String class's replaceAll() method also accepts regex patterns just like the split() method.
public class PatternSyntaxExceptionExample2 { public static void main(String args[]) { String str = "Be + in + Present";System.out.println(str.replaceAll("+","x"));} }
Output:
Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '+' near index 0
+
^
Explanation
Similar to the split() method, the replaceAll() method also accepts a regular expression pattern, we need to escape the metacharacters(such as +,*,?) to resolve the issue.
Solution
The escape character in the replaceAll() method should be used as "\\+" as shown below in the example:
public class PatternSyntaxExceptionExample2 { public static void main(String args[]) { String str = "Be + in + Present";System.out.println(str.replaceAll("\\+","x"));} }
Output:
Be x in x Present
Example 3: Producing the exception due to incorrect usage of metacharacters
You will get this exception even if we are not trying to match any metacharacter. But still, the code will throw an exception due to incorrect usage of metacharacters i.e. '+' as shown in the below example:
import java.util.regex.*; public class PatternSyntaxExceptionExample3 { public static void main(String args[]) {Pattern pattern = Pattern.compile("+\\[0-9\\]");String[] items = pattern.split("[0123456789],[67890],[7654321]"); for (String item : items) { System.out.println(item); } } }
Output:
Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '+' near index 0
+
^
Explanation
In the above example, we can easily fix the exception by correcting the regex pattern.
Solution
Use "\\+" instead of "+" in the regex pattern as shown below in the example:
import java.util.regex.*; public class PatternSyntaxExceptionExample3 { public static void main(String args[]) {Pattern pattern = Pattern.compile("\\+\\[0-9\\]");String[] items = pattern.split("[0123456789],[67890],[7654321]"); for (String item : items) { System.out.println(item); } } }
Output:
[0123456789],[67890],[7654321]
That's all for today. Please mention in the comments in case you are still facing the java.util.regex.PatternSyntaxException: dangling meta character '+' near index 0 exception in Java.