gpt4 book ai didi

java - java项目的正则表达式

转载 作者:行者123 更新时间:2023-12-01 16:32:08 25 4
gpt4 key购买 nike

我将如何为以下示例执行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 的所有 € 值。

最佳答案

只是..通读你的代码,把它当作学期论文,你当然也校对过它们:

  1. 您正在调用 veriableNamePattern.matches(veriableNamePattern) - 当然,它不会。您想要匹配 veriableName

  2. 在这两种情况下都打印 studentID + "is valid.",因此无法区分。大概您想在第二个 println 行中的某处引入 not

关于java - java项目的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62019983/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com