The default separator for the scanner is spaces, so the first (and only) token in your scanner object is the whole string "*(,identifier1*(identifier2" . This is the string you are trying to get by calling next("[\\w]+") , which throws an exception because it does not match your input.
Why do you prefer findInLine("\\w+") :
Scanner scan = new Scanner("*(,identifier1*(identifier2"); System.out.println(scan.findInLine("\\w+")); System.out.println(scan.findInLine("\\w+"));
which produces:
identifier1 identifier2
Or, if you want to split the input string into one or more alpha characters (ascii) alpha-num-chars (and _ ), try:
Scanner scan = new Scanner("*(,identifier1*(identifier2").useDelimiter("\\W+"); while(scan.hasNext()) { System.out.println(scan.next()); }
which produces the same conclusion as before.
Note that I used capital W , which is equal to:
\W == [^\w] == [^a-zA-Z0-9_]
source share