Java Scanner with regex delimiter

2019-05-21 11:08发布

Why does the following code return false?

Scanner sc = new Scanner("-v ");
sc.useDelimiter("-[a-zA-Z]\\s+");
System.out.println(sc.hasNext());

The weird thing is -[a-zA-Z]//s+ will return true.

I also can't understand why this returns true:

Scanner sc = new Scanner(" -v");
sc.useDelimiter("-[a-zA-Z]\\s+");
System.out.println(sc.hasNext());

2条回答
甜甜的少女心
2楼-- · 2019-05-21 11:35

A scanner is used to break up a string into tokens. Delimiters are the separators between tokens. The delimiters are what aren't matched by the scanner; they're discarded. You're telling the scanner that -[a-zA-Z]\\s+ is a delimiter and since -v matches that regex it skips it.

If you're looking for a string that matches the regex, use String.matches().

If your goal really is to split a string into tokens then you might also consider String.split(), which is sometimes more convenient to use.

查看更多
SAY GOODBYE
3楼-- · 2019-05-21 11:41

Thanks John Kugelman, I think you're right.

Scanner can use customized delimiter to split input into tokens. The default delimiter is a whitespace.

When delimiter doesn't match any input, it'll result all the input as one token:

    Scanner sc = new Scanner("-v");
    sc.useDelimiter( "-[a-zA-Z]\\s+");
     if(sc.hasNext())
          System. out.println(sc.next());

In the code above, the delimiter actually doesn't get any match, all the input "-v" will be the single token. hasNext() means has next token.

    Scanner sc = new Scanner( "-v ");
    sc.useDelimiter( "-[a-zA-Z]\\s+");
     if(sc.hasNext())
          System. out.println(sc.next());

this will match the delimiter, and the split ended up with 0 token, so the hasNext() is false.

查看更多
登录 后发表回答