title: Why isn't [\\s*] equivalent to \\s*?
body: <p>I'm just getting into regex in Java, I'm reading a book and the Java docs - and I can't figure why - given the following program - that <code>"[\\s*]"</code> is not equivalent to <code>"\\s*"</code> when used as a delimiter. It seems <code>"[\\s*]"</code> is equivalent to <code>"\\s+"</code>, can someone walk me through logically why this is so?</p>
  
  <pre><code>import java.util.Scanner;
  import java.util.regex.Pattern;
  public class ScanString {
      public static void main(String[] args) {
          String str = "Smith , where Jones had had 'had', had had 'had had'.";
          String regex = "had";
          System.out.println("String is:\n" + str + "\nToken sought is " + regex);
  
          Pattern had = Pattern.compile(regex);
          Scanner strScan = new Scanner(str);
          strScan.useDelimiter("\\s*");
          int hadCount = 0;
          while(strScan.hasNext()) {
              if(strScan.hasNext(had)) {
                  ++hadCount;
                  System.out.println("Token found!: " + strScan.next(had));
  
              } else {
                  System.out.println("Token is    : " + strScan.next());
              }
          }
          System.out.println("Count is: " + hadCount);
      }
  }</code></pre>
  
  <p>The output, which makes sense to me, is every non-whitespace character as a separate token. When the delimiter is changed to <code>"\\s+"</code> or <code>"[\\s*]"</code> , the output is:</p>
  
  <p><code><pre>String is:
  Smith , where Jones had had 'had', had had 'had had'.
  Token sought is had
  Token is    : Smith
  Token is    : ,
  Token is    : where
  Token is    : Jones
  Token found!: had
  Token found!: had
  Token is    : 'had',
  Token found!: had
  Token found!: had
  Token is    : 'had
  Token is    : had'.
  Count is: 4</code></pre></p>