RSS

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

07 Sep

You can get this error when you try to use the split method in java like

String str = “sample?string”;

str.split(“?”);

here the split will not work in usual manner where it should return an array with two elements, sample and string… Instead it will throw the above error. This is because few characters are reserved characters which include “?” pattern also.. Hence it cannot be parsed by split method.. So we should use the escape sequence as

str.split(“\\?”);

which should work fine

 
Leave a comment

Posted by on September 7, 2011 in Java

 

Tags: ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: