1

What does [.\\d\\D]* mean.

I am trying to check a valid java main method statement by

java.matches("[.\\d\\D]*((public)\\s(static)\\s(void)\\s(main)\\((String)\\[\\]\\s(args)\\))[.\\d\\D]*");

what does that part mean?

Eduard Wirch
  • 9,785
  • 9
  • 61
  • 73
gatsbyz
  • 1,057
  • 1
  • 10
  • 26

1 Answers1

4

Well [.\d\D]* means match:

0 or more of anyone of these properties

  • digit
  • non-digit
  • literal dot

IMO this is not really required since this can effectively match anything and is equivalent of .* with DOTALL switch.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • does .* also include the new line (\n)? One of my test cases ("\n // ///*\"public static void main(String[] args)\"*/ \n ") is failing. – gatsbyz Nov 24 '13 at 07:18
  • It is indeed odd. Even without DOTALL, `[\d\D]`, `[\s\S]`, or `[\w\W]` (i.e. no `.`) are the forms I've seen. The extra `.` makes it look like the original author just started throwing things together at random. – user2864740 Nov 24 '13 at 07:18
  • @SWKING It's odd because the `.` means nothing there (as it is covered by `[\d\D]`). Anyway, see DOTALL: http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#DOTALL – user2864740 Nov 24 '13 at 07:20
  • DOT will not match newline unless you use `(?s)` in front of your regex like `"a\nb".matches("(?s).*");` OR `Pattern.DOTALL` flag – anubhava Nov 24 '13 at 07:21