【发布时间】:2016-05-27 21:50:45
【问题描述】:
我在尝试为我的代码查找正则表达式时遇到了一些问题。这里是:
Scanner key = new Scanner(System.in);
//this is the variable
int s = 4;
String input = "";
String bregex = "[1-9][0-9]{1," + (s*s) + "}";
boolean cfgmatch = false;
while(cfgmatch == false){
input = key.next();
Pattern cfgbp = Pattern.compile(bregex);
Matcher bm = cfgbp.matcher(input);
if(bm.matches()){
System.out.println("working");
}
else{
System.out.println("not working");
}
}
我正在尝试制作一个正则表达式来限制板上的多个单元格。单元格数不能大于板的空间,即“s*s”。
示例:如果板子的大小是 4,输入可以是 1 到 16,如果是 5,则可以是 1 到 25,等等...
Board size 只能是 1 到 9。
我写过这段话是为了在输入失败的情况下要求另一个数字。
【问题讨论】:
-
你为什么使用字符串工具,正则表达式,因为听起来最好通过简单的数字布尔检查来处理?看起来您正在尝试使用螺丝刀来锯断木板 - 您使用了错误的工具来完成这项工作。
-
我猜Zawinski 在他写道:“有些人遇到问题时,会想‘我知道,我会使用正则表达式。’”现在他们有两个问题。”