【发布时间】:2016-01-28 22:00:30
【问题描述】:
我的导师分配了一个正则表达式问题,他希望我们通过更改三个声明变量中的字符串值来使所有返回值都为真。这是我第一次做正则表达式问题,如果可以的话,我需要一点帮助。我试过 www.regexpal.com 但我不知道如何使用它。 关于我如何开始解决这个问题,有人可以对这个话题有所了解吗?谢谢
以下代码:
public class RegexTester {
public static void main(String[] args) {
String regexSSN = ""; //TODO add a regex for Social Security Numbers
String regex9000 = ""; //TODO add a regex for GGC 9000 numbers here
String regexZipPlus4 = ""; //TODO add a regex for zip+4 zipcodes here
System.out.println("All of the following tests shoule return true, "
+ "the negative tests are negated (meaning that they should "
+ "also return true)");
System.out.println("192-192-5555".matches(regexSSN)); // the following tests should all match
System.out.println("444-183-1212".matches(regexSSN));
System.out.println("032-431-9375".matches(regexSSN));
System.out.println("122-650-4343".matches(regexSSN));
System.out.println("900012389".matches(regex9000));
System.out.println("900112389".matches(regex9000));
System.out.println("900012390".matches(regex9000));
System.out.println("900050000".matches(regex9000));
System.out.println("30043".matches(regexZipPlus4));
System.out.println("30043-1234".matches(regexZipPlus4));
System.out.println(); // the following codes print out true
System.out.println(!"192-XYZ-5555".matches(regexSSN)); // the following tests should NOT match
System.out.println(!"00-192-5555".matches(regexSSN));
System.out.println(!"90005000".matches(regex9000)); // too short!
System.out.println(!"900250000".matches(regex9000)); // must be 9000* or 9001*
System.out.println(!"9002500001".matches(regex9000)); // to big
System.out.println(!"9001FOO00".matches(regex9000)); // no alpha allowed
System.out.println(!"30043-12345".matches(regexZipPlus4)); // too long
System.out.println(!"300430-1234".matches(regexZipPlus4)); // too long
System.out.println(!"30043-12".matches(regexZipPlus4)); // too short
System.out.println(!"300FO-1234".matches(regexZipPlus4)); // no alpha allowed
System.out.println(!"30043=1234".matches(regexZipPlus4)); // missing hyphen
}
}
【问题讨论】:
-
基本上:你能做我的功课吗?简短回答:否。
-
我不是想让人们做我的作业,我只是需要帮助解决这个问题,因为我不知道如何解决它。也许与其攻击我,不如给我指出我可以解决它的方向。
-
regex101.com 可能有助于设计您的正则表达式字符串
-
@xtremeslice 我知道您似乎受到了攻击,但我想向您保证这并不是真正发生的事情。您提出问题的方式并不是真正的 SO 运作方式。从尝试第一部分开始,当您有一个关于您的解决方案为什么不起作用的具体问题时,然后回来寻求帮助。
-
哦,好吧,我试着弄清楚。谢谢