带有正则表达式分隔符的 Java Scanner

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15489570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 19:46:10  来源:igfitidea点击:

Java Scanner with regex delimiter

javajava.util.scanner

提问by Sawyer

Why does the following code return false?

为什么下面的代码返回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.

奇怪的是-[a-zA-Z]//s+会返回true。

I also can't understand why this returns true:

我也不明白为什么这会返回 true:

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

回答by John Kugelman

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

扫描器用于将字符串分解为标记。分隔符是分隔标记之间。分隔符是扫描仪匹配的内容;他们被丢弃了。您告诉扫描仪这-[a-zA-Z]\\s+是一个分隔符,并且由于-v匹配该正则表达式,因此它会跳过它。

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

如果您正在寻找与正则表达式匹配的字符串,请使用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.

如果您的目标确实是将字符串拆分为标记,那么您也可以考虑String.split(),有时使用起来更方便。

回答by Sawyer

Thanks John Kugelman, I think you're right.

谢谢约翰·库格曼,我认为你是对的。

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

Scanner 可以使用自定义分隔符将输入拆分为令牌。默认分隔符是空格。

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.

在上面的代码中,分隔符实际上没有得到任何匹配,所有输入“-v”都将是单个标记。 hasNext() 表示有下一个标记

    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.

这将匹配分隔符,并且拆分以 0 标记结束,因此 hasNext() 为 false。