Java Regex PatternSyntaxException

String inputString=”1.I am a code error resolver\n”+”2.Here I am resolving a regex exception\n”+”3.You need to keep in mind while using the regex or special characters in java\n”+”4.Some of the special symbols are reserved”;

String newString=inputString.replaceAll(“\n”,”**”);

String anotherNewString=newString.replaceAll(“**”,”\n”);

will throw an Exception as ** is reserved in Java.

The exception thrown will be :

Exception in thread “main” java.util.regex.PatternSyntaxException: Dangling meta character ‘*’ near index 0 **

So use other characters or symbols other than * and $ as they are reserved and if used for string replacement will throw the dangling meta character Exception.

 

 

 

Leave a Comment

Your email address will not be published.