java.util.regex.PatternSyntaxException: Dangling meta character

When you try to split any string with ? or * as below code.

String sqlParts[] = sql.split("?");

You will end up with unchecked PatternSyntaxException as given below.

java.util.regex.PatternSyntaxException: Dangling meta character '?' near index 0

Solution:
Avoid using dangling metacharacters like ‘?’, ‘+’ and ‘*’. Instead use it with escape sequences as like below.

String sqlParts[] = sql.split("\\?");

One thought on “java.util.regex.PatternSyntaxException: Dangling meta character”

Leave a Reply

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