我将如何为以下示例执行java正则表达式,每个示例都被扫描仪作为字符串接收,并且只能在扫描仪接收每个部分之后设置正则表达式,因此可以在扫描仪之后设置正则表达式,例如
Scanner scan = new Scanner(System.in);
String veriableName = scan.nextLine();
String studentID = scan.nextLine();
String word = scan.nextLine();
................
//regex can only be set here
String veriableNamePattern = "";
String studentIDPattern="";
String wordPattern="";
.............
if(veriableNamePattern.matches(veriableNamePattern ){
System.out.println(veriableName + " is valid.");
}
else{
System.out.println(studentID + " is valid.");
}
以下是我尝试做的示例: 由一些字母字符后跟任意字母或数字序列组成的变量名称。
学生证号码,由 7 位数字表示,必须以 1 开头,以字母 s 结尾。
任何以“ed”结尾的四个字母的单词。
由两位数字后跟三个大写字母表示的产品代码。
在行首查找从 €100 到 €999 的所有 € 值。
最佳答案
只是..通读你的代码,把它当作学期论文,你当然也校对过它们:
您正在调用
veriableNamePattern.matches(veriableNamePattern)
- 当然,它不会。您想要匹配veriableName
。在这两种情况下都打印
studentID + "is valid."
,因此无法区分。大概您想在第二个 println 行中的某处引入not
。
关于java - java项目的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62019983/