我想创建一个正则表达式,它可以帮助我摆脱以下代码-
public class Test {
public static void main(String[] args) {
String test = "1026";
int testToInt = 0;
if(checkIfInteger(test))
testToInt = Integer.parseInt(test);
if(testToInt >= 1024 && testToInt <= 65535)
System.out.println("Validity is perfect");
else
System.out.println("Validity is WRONG");
}
public static boolean checkIfInteger(String givenString) {
boolean check = false;
for(int i = 0; i < givenString.length(); i++) {
if(givenString.charAt(i) >= '0' && givenString.charAt(i) >= '9')
check = true;
else {
check = false;
break;
}
}
return check;
}
}
基本上,它检查一个字符串是否只包含数字,以及它的范围是否在1024到65535之间。
为此,我创建了以下正则表达式-
"\b(102[4-9]|10[3-9][0-9]|1[1-9][0-9]{2}|[2-9][0-9]{3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[01][0-9]|6552[0-5])\b"
但它有很多值是失败的。有人能给我一个更聪明/正确的方法吗?
如果你想测试你的正则表达式,这里有一个测试文件-
public class Test {
public static void main(String[] args) {
for (int i = 0; i < 1024; i++) {
if (String
.valueOf(i)
.matches(
"\b(102[4-9]|10[3-9][0-9]|1[1-9][0-9]{2}|[2-9][0-9]{3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[01][0-9]|6552[0-5])\b"))
System.out.println("Hum " + i);
}
for (int i = 1025; i < (int) Math.pow(2, 16); i++) {
if (!String
.valueOf(i)
.matches(
"\b(102[4-9]|10[3-9][0-9]|1[1-9][0-9]{2}|[2-9][0-9]{3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[01][0-9]|6552[0-5])\b"))
System.out.println("Hum " + i);
}
for (int i = 0; i < 100; i++) {
if (String
.valueOf((int)Math.pow(2, 16) + i)
.matches(
"\b(102[4-9]|10[3-9][0-9]|1[1-9][0-9]{2}|[2-9][0-9]{3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[01][0-9]|6552[0-5])\b"))
System.out.println("Hum " + i);
}
}
}
发布于 2015-04-08 01:04:23
更改您的代码
来自:的
testToInt = Integer.parseInt(test);
if(testToInt >= 1024 && testToInt <= 65535)
System.out.println("Validity is perfect");
else
System.out.println("Validity is WRONG");
To:
try {
testToInt = Integer.parseInt(test);
if(testToInt >= 1024 && testToInt <= 65535)
System.out.println("Validity is perfect");
else
System.out.println("Validity is WRONG");
}
catch(NumberFormatException nfe)
{
System.out.println("Validity is WRONG");
}
发布于 2015-04-08 01:03:40
在Java中,您需要使用双重转义符号,因此在修复该位之后,您的正则表达式字符串将如下所示:
String pattern = "\\b(102[4-9]|10[3-9][0-9]|1[1-9][0-9]{2}|[2-9][0-9]{3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[01][0-9]|6552[0-5])\\b";
这已经修复了很多,我只得到了这些“嗡嗡”:
Hum 65526
Hum 65527
Hum 65528
Hum 65529
Hum 65530
Hum 65531
Hum 65532
Hum 65533
Hum 65534
Hum 65535
现在,添加|6553[0-5]
,我得到了一个完全工作的正则表达式:
String pattern = "\\b(102[4-9]|10[3-9][0-9]|1[1-9][0-9]{2}|[2-9][0-9]{3}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[012][0-9]|6552[0-5]|6553[0-5])\\b";
example program based on your testing code is available here。
发布于 2015-04-08 01:04:11
在这里抛出一个Exception
比返回一个布尔值更好。
类似于:
public int parseAndCheck(String val, int low, int high) throws IllegalArgumentException {
try {
int num = Integer.parseInt(val);
if (num < low || num > high) throw new IllegalArgumentException(val);
return num;
}
catch (NumberFormatException ex) {
throw new IllegalArgumentException(ex);
}
}
https://stackoverflow.com/questions/29496892
复制相似问题