Java:扫描字符串以获取模式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2390500/
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
Java: scanning string for a pattern
提问by Markos Fragkakis
This is probably a quicky. Why does this code not return anything?
这可能是一个快速的。为什么此代码不返回任何内容?
import java.util.Scanner;
public class MainClass {
public static void main(String[] args) {
try {
Scanner sc = new Scanner("asda ASA adad");
String pattern = "[A-Z]+";
while ((sc.hasNext(pattern))) {
System.out.println(sc.next(pattern));
}
sc.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
采纳答案by polygenelubricants
hasNext(String pattern)
only returns true
if the next token matches the pattern. In your case, "asda"
is the next token, and that does NOT match "[A-Z]+"
. The documentation is clear in that "[the] scanner does not advance past any input".
hasNext(String pattern)
仅true
当下一个标记与模式匹配时才返回。在您的情况下,"asda"
是下一个令牌,与"[A-Z]+"
. 文档很清楚,“[the] 扫描仪不会超过任何输入”。
If you change the pattern to "[A-Za-z]+"
, then you'd get three tokens, which may be what you intended.
如果您将模式更改为"[A-Za-z]+"
,那么您将获得三个令牌,这可能正是您想要的。
If in fact you only want to get tokens that match "[A-Z]+"
, then you can do any of the following:
如果实际上您只想获得 match 的令牌"[A-Z]+"
,那么您可以执行以下任一操作:
- simply discard non-matching tokens
useDelimiter("[^A-Z]+")
, then simply invokenext()
- use
skip("[^A-Z]+")
- use
findInLine("[A-Z]+")
- 简单地丢弃不匹配的令牌
useDelimiter("[^A-Z]+")
,然后简单地调用next()
- 用
skip("[^A-Z]+")
- 用
findInLine("[A-Z]+")
Tip: if performance is critical, you'd want to use the precompiled Pattern
overloads of these methods.
提示:如果性能很关键,您会想要使用Pattern
这些方法的预编译重载。
Tip: do keep in mind that"Xooo ABC"
has two "[A-Z]+"
matches. If this is not what you want, then the regex will have to be a bit more complicated. Or you can always simply discard non-matching tokens.
提示:请记住,"Xooo ABC"
有两个"[A-Z]+"
匹配项。如果这不是您想要的,那么正则表达式将不得不更复杂一些。或者你总是可以简单地丢弃不匹配的令牌。
回答by Chris B.
Change
改变
String pattern = "[A-Z]+";
to
到
String pattern = "[a-zA-Z]+";
回答by Dark Castle
If you are looking to print out all words surrounded by your delimiter you might want to be safe and exclude the pattern altogether. That way you don't come across a word that contains a character not in your pattern which would cause your program exit that loop (as it's currently doing). For example:
如果您想打印出由分隔符包围的所有单词,您可能希望安全并完全排除该模式。这样您就不会遇到包含不在您的模式中的字符的单词,这会导致您的程序退出该循环(正如它目前所做的那样)。例如:
while ((sc.hasNext())) {
System.out.println(sc.next());
}