Java 如何匹配任意数量的大写字母?
为什么obove代码会引发java.lang.IllegalStateException?如何匹配任意数量的大写字母?您需要调用Java 如何匹配任意数量的大写字母?,java,regex,Java,Regex,为什么obove代码会引发java.lang.IllegalStateException?如何匹配任意数量的大写字母?您需要调用Matcher.find()来启动正则表达式匹配过程 public static void main(String[] args) { Pattern p = Pattern.compile("[A-Z]*"); Matcher matcher = p.matcher("CSE"); System.out.println(matcher.grou
Matcher.find()
来启动正则表达式匹配过程
public static void main(String[] args) {
Pattern p = Pattern.compile("[A-Z]*");
Matcher matcher = p.matcher("CSE");
System.out.println(matcher.group());
}
您需要调用
Matcher.find()
来启动正则表达式匹配过程
public static void main(String[] args) {
Pattern p = Pattern.compile("[A-Z]*");
Matcher matcher = p.matcher("CSE");
System.out.println(matcher.group());
}
您必须调用
matcher.matches()调用matcher.group()之前的代码>代码>
matcher.group()
为您提供上一个匹配所标识的子字符串
你的模式应该是[A-Z]+
。这将打印所有匹配的大写字母序列
public static void main(String[] args)
{
Pattern p = Pattern.compile("[A-Z]*");
Matcher matcher = p.matcher("CSE");
while (matcher.find()) {
System.out.println(matcher.group());
}
}
您必须调用matcher.matches()调用matcher.group()之前的代码>代码>
matcher.group()
为您提供上一个匹配所标识的子字符串
你的模式应该是[A-Z]+
。这将打印所有匹配的大写字母序列
public static void main(String[] args)
{
Pattern p = Pattern.compile("[A-Z]*");
Matcher matcher = p.matcher("CSE");
while (matcher.find()) {
System.out.println(matcher.group());
}
}
是否必须至少有一个资本。如果您需要+
而不是*
,则至少必须有一个大写字母。如果是这样,您需要+
而不是*