java.util.regex.PatternSyntaxException: Dangling meta character ‘*’ near index 0

The regular expression contains meta characters such as “*”, “+”, “?”. The exception “java.util.regex.PatternSyntaxException: Dangling meta character” will be thrown when using these regular expression meta characters in java methods

You’ll see the exception as below 

Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 0
*
^
	at java.util.regex.Pattern.error(Pattern.java:1955)
	at java.util.regex.Pattern.sequence(Pattern.java:2123)
	at java.util.regex.Pattern.expr(Pattern.java:1996)
	at java.util.regex.Pattern.compile(Pattern.java:1696)
	at java.util.regex.Pattern.<init>(Pattern.java:1351)
	at java.util.regex.Pattern.compile(Pattern.java:1028)
	at java.lang.String.split(String.java:2380)
	at java.lang.String.split(String.java:2422)
	at com.yawintutor.StringSplit.main(StringSplit.java:9)
Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '+' near index 0
+
^
Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '?' near index 0
?
^

Root Cause

The characters “*”, “+”, “?” are called meta characters in regular expression. These meta characters are used to match more than one occurrence of the matching pattern in a string. These characters are used as literals using the escape characters. The following are the meaning of these meta characters

Meta CharacterDescriptionEscape character
*zero or more occurrence of the characters\\*
+one or more occurrence of the characters\\+
?zero or only one occurrence of the characters\\?
^Start of the character sequence\\^
$End of the character sequence\\$

How to reproduce this issue

If The meta characters “*”, “+”, “?” are used within the regular expression for the purpose of matching the literal characters, the exception java.util.regex.PatternSyntaxException will be thrown

package com.yawintutor;

public class StringSplit {
	public static void main(String[] args) {
		String str = "Yawin*tutor";
		String str2 = "*";
		String[] strArray;

		strArray = str.split(str2);

		System.out.println("Given String : " + str);
		for(int i=0;i<strArray.length;i++) {
			System.out.println("strArray["+i+"] = " + strArray[i]);
		}
	}
}

Solution

The meta characters “*”, “+”, “?” are used within the regular expression with escape character will resolve this issue. the escape characters are “\\*”, “\\+” and “\\?”. The example below shows how to use the escape character within regular expression string in java.

package com.yawintutor;

public class StringSplit {
	public static void main(String[] args) {
		String str = "Yawin*tutor";
		String str2 = "\\*";
		String[] strArray;

		strArray = str.split(str2);

		System.out.println("Given String : " + str);
		for(int i=0;i<strArray.length;i++) {
			System.out.println("strArray["+i+"] = " + strArray[i]);
		}
	}
}

Output

Given String : Yawin*tutor
strArray[0] = Yawin
strArray[1] = tutor

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *