java.util.regex.PatternSyntaxException Dangling meta character near

java.util.regex.PatternSyntaxException Dangling meta character near

OR
Exception in thread main java.util.regex.PatternSyntaxException Dangling meta character * near index 2

Exception in thread "main" java.util.regex.PatternSyntaxException: Dangling meta character '*' near index 2
a**b
  ^
    at java.util.regex.Pattern.error(Pattern.java:1713)
    at java.util.regex.Pattern.sequence(Pattern.java:1878)
    at java.util.regex.Pattern.expr(Pattern.java:1752)
    at java.util.regex.Pattern.compile(Pattern.java:1460)
    at java.util.regex.Pattern.<init>(Pattern.java:1133)
    at java.util.regex.Pattern.compile(Pattern.java:823)
    at com.javahonk.patternmatch.PatternMatcherTest.main(PatternMatcherTest.java:10)

If you are getting above exception when doing pattern match means regex which you try to match is not correct. Above exception happened because there was extra * in expression marcher below:

Pattern p = Pattern.compile("a**b");

Remove extra * from it will fix the issue shown below:

Pattern p = Pattern.compile("a*b");

Leave a Reply

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