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

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

The same error comes when the regex is “+”

Here is the solution.

Enclose your * and + within square brackets.

Here is my code.

public static final String [] wildCards={"[*]","%","@","&","[+]"};

public static void main(String[] args) {
String s="!@#$%^&*()";
s=stripWild(s, wildCards);
System.out.println(s);
}

  1. July 27th, 2009 at 12:08 | #1

    No use

  2. Shahul
    August 20th, 2009 at 10:24 | #2

    Worked like a charm !!! Thx !!

  3. Shridevi
    August 29th, 2009 at 06:55 | #3

    Thanks :)

  4. Kosta
    October 11th, 2009 at 12:48 | #4

    Thanx, you have saved me hours :>

  5. dls
    November 19th, 2009 at 20:07 | #5

    Worked for mee….. Thanks!

  6. manish
    January 13th, 2010 at 17:38 | #6

    thanx, it worked for me. I was having issue with “?”, it get solved after using “[?]“.

  7. Siam
    January 14th, 2010 at 21:17 | #7

    Thanks! It worked perfecly with split()
    String[] temp = stringVar.split(“[+]“);

  8. mat
    March 6th, 2010 at 23:43 | #8

    *bow*
    thanks, it helped me with my Strint.split() problem

  9. Sri
    March 26th, 2010 at 03:10 | #9

    Thanks .. this was helpful

  10. May 25th, 2010 at 15:55 | #10

    getSomeString().replaceAll(“*”, “.*”) wont work either, with the same error message. use getSomeString().replaceAll(“[*]“, “.*”) instead.

    Greetings, thanks for the advice!

  11. Uday
    July 26th, 2010 at 15:03 | #11

    Thanks it was helpful to me. can you explain why it is giving error by without enclosing “*” with brackets

  1. No trackbacks yet.