使用 java.util.scanner.hasNext(regex) 获取整行

发布于 2024-08-29 17:19:17 字数 419 浏览 10 评论 0原文

我正在用 Java 做一些事情,要求输入与模式 ^[1-5]$ 相匹配。我应该有一个 while 循环遍历每一行输入,根据模式检查它,如果没有,则输出错误消息。

Sudo 代码:

while (regex_match(/^[^1-5]$/,inputLine)) {  
    print ("Please enter a number between 1 and 5! ");  
    getNextInputLine();
}  

我可以使用 java.util.Scanner.hasMatch("^[^1-5]$"),但这只会匹配单个标记,而不是整行。关于如何使 hasMatch 与整行匹配有什么想法吗? (将分隔符设置为“\n”或“\0”不起作用。)

编辑:如果这不可能,是否还有其他方法可以做到这一点?

I'm doing something in Java that requires input to be matched against the pattern ^[1-5]$. I should have a while loop looping through each line of input, checking it against the pattern, and outputting an error message if it does not.

Sudo code:

while (regex_match(/^[^1-5]$/,inputLine)) {  
    print ("Please enter a number between 1 and 5! ");  
    getNextInputLine();
}  

I can use java.util.Scanner.hasMatch("^[^1-5]$"), but that will only match a single token, not the entire line. Any idea on how to make hasMatch match against the entire line? (Setting the delimiter to "\n" or "\0" doesn't work.)

Edit: If this isn't possible, is there some other way to do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

那小子欠揍 2024-09-05 17:19:17

目前尚不清楚你到底在问什么。

  • hasNextInt () 可以告诉您是否有 int nextInt()。然后您可以检查它是否在 1-5 之间,以及是否不提示输入另一个。这似乎是最干净的选项,甚至不需要使用正则表达式。

  • hasNext ("[1-5]") 可以告诉您下一个标记的整体是否是 1-5 之间的 nextInt()。这实际上有点严格,因为如果下一个标记是 "005",则它不匹配。您可以使正则表达式更加复杂以适应这种情况,但此时上面的第一个选项变得更具吸引力。

  • findInLine(String pattern) 可以查找模式,忽略分隔符,跳过任何不匹配的输入。例如,您可以从诸如“我想买 3 个苹果,请!”之类的输入中提取数字。这可能不是您所需要的(但从问题中尚不清楚)。


另一种可能性是,也许您不仅需要下一个标记为 ^[1-5]$,而且您希望整个下一行与之匹配。如果是这种情况,那么也许您需要这样的东西:

String line = scanner.nextLine();
if (!line.matches("^[1-5]$")) {
  // error message
} else {
  int num = Integer.parseInt(line);
}

如果您想检查 nextLine() 是否与该模式匹配而不消耗该行,那么您可以执行以下操作:

if (scanner.findInLine("^[1-5]$") != null) {
  // nextLine().matches("[1-5]");
}

It's not clear what you're asking exactly.

  • hasNextInt() can tell you if there's an int nextInt(). You can then check if it's between 1-5, and if it isn't prompt for another. This would seem to be the cleanest option, without even using regex.

  • hasNext("[1-5]") can tell you if the entirety of the next token is a nextInt() between 1-5. This is actually a bit strict in that if the next token is, say, "005", it doesn't match. You can make the regex more complicated to accommodate this, but at that point the first option above becomes even more attractive.

  • findInLine(String pattern) can find a pattern, ignoring delimiters, skipping any input that doesn't match. You can extract the number from an input like "I want to buy 3 apples, please!", for example. This likely isn't what you need (but it isn't clear from the question).


Another possibility is that perhaps not only do you need the next token to be ^[1-5]$, but you want the entire next line to match that. If that's the case, then maybe something like this is what you need:

String line = scanner.nextLine();
if (!line.matches("^[1-5]$")) {
  // error message
} else {
  int num = Integer.parseInt(line);
}

If you want to check if the nextLine() matches that pattern without consuming that line, then you can do the following:

if (scanner.findInLine("^[1-5]$") != null) {
  // nextLine().matches("[1-5]");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文