Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does "hello\\s*world" not match "hello world"?

Why does this code throw a InputMismatchException ?

Scanner scanner = new Scanner("hello world");
System.out.println(scanner.next("hello\\s*world"));

The same regex matches in http://regexpal.com/ (with \s instead of \\s)

like image 875
Navin Avatar asked Jun 10 '11 17:06

Navin


3 Answers

A Scanner, as opposed to a Matcher, has built in tokenization of the string, the default delimiter is white space. So your "hello world" is getting tokenized into "hello" "world" before the match runs. It would be a match if you changed the delimiter before scanning to something not in the string, eg.:

Scanner scanner = new Scanner("hello world");
scanner.useDelimiter(":");
System.out.println(scanner.next("hello\\s*world"));

but it seems like really for your case you should just be using a Matcher.

This is an example of using a Scanner "as intended":

   Scanner scanner = new Scanner("hello,world,goodnight,moon");
   scanner.useDelimiter(",");
   while (scanner.hasNext()) {
     System.out.println(scanner.next("\\w*"));
   }

output would be

hello
world
goodnight
moon
like image 167
Affe Avatar answered Oct 19 '22 06:10

Affe


The default delimiter of a scanner are whitespaces, so the scanner sees two elements hello and world. And hello\s+world is not matching hello therefore a NoSuchElement exception is thrown.

like image 34
dmeister Avatar answered Oct 19 '22 05:10

dmeister


These inputs work:

"C:\Program Files\Java\jdk1.6.0_21\bin\java"  RegexTest hello\s+world "hello      world"
'hello      world' does match 'hello\s+world'

Here's the code:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class RegexTest {

    public static void main(String[] args) {

        if (args.length > 0) {
            Pattern pattern = Pattern.compile(args[0]);

            for (int i = 1; i < args.length; ++i) {
                Matcher matcher = pattern.matcher(args[i]);
                System.out.println("'" + args[i] + "' does " + (matcher.matches() ? "" : "not ") + "match '" + args[0]  +"'");
            }
        }
    }

}
like image 2
duffymo Avatar answered Oct 19 '22 06:10

duffymo