Java 为什么在使用模式类识别字符串是否有效时出错?
为什么要在//2//3中使用Java 为什么在使用模式类识别字符串是否有效时出错?,java,regex,Java,Regex,为什么要在//2//3中使用s[0] 显示的输出 package app; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ValidIdentifierDemo { public static void main(String s[]) { Pattern p=new Pattern.compile("[a-z A-z][a-z A-Z 0-9]+");
s[0]
显示的输出
package app;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class ValidIdentifierDemo
{
public static void main(String s[])
{
Pattern p=new Pattern.compile("[a-z A-z][a-z A-Z 0-9]+"); // error here
Matcher m=p.matcher("int"); //2 or s[0] in place of int?
if (m.find() && m.group().equals(s[0])) // 3 why s[0] shouldn't i use "int"
{
System.out.println("Valid");
}
else
{
System.out.println("InValid");
}
}
}
你必须做到:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
at app.ValidIdentifierDemo.main(ValidIdentifierDemo.java:11)
i、 e.删除new
关键字,因为.compile()
方法将为您生成模式。您必须执行以下操作:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException
at app.ValidIdentifierDemo.main(ValidIdentifierDemo.java:11)
i、 e.删除new
关键字,因为.compile()
方法将为您生成模式。删除new
关键字
Pattern p = Pattern.compile("[a-z A-z][a-z A-Z 0-9]+");
模式
的构造函数不可访问,新的模式
通过静态调用模式.compile(字符串模式)
初始化
调用构造函数还需要在构造函数名称后加上括号,如果适用,括号之间还需要一系列参数 删除new
关键字
Pattern p = Pattern.compile("[a-z A-z][a-z A-Z 0-9]+");
模式
的构造函数不可访问,新的模式
通过静态调用模式.compile(字符串模式)
初始化
调用构造函数还需要在构造函数名称后加上括号,如果适用,括号之间还需要一系列参数 顺便说一句,[a-z a-z]
允许文本中有空格。顺便说一句,[a-z a-z]
允许文本中有空格。