怎样拿到Matcher m=pattern.matcher(str);里分组的值
String str ="${time}";
String req="\\$\\{([a-z]{4})\\}";
Pattern pattern=Pattern.compile(req);
Matcher m=pattern.matcher(str);
String c = m.group(1)
boolean s=m.find();
System.out.println(c);
怎样拿到Matcher m=pattern.matcher(str);里分组的值 也就是字符串里time的值 String c = m.group(1)拿不到 报java.lang.IllegalStateException: No match found错
[解决办法]
要先调用m.find()后才能从group里面取值,你的程序正好写反了,你是先group,然后才调用find的
import java.util.regex.Matcher;import java.util.regex.Pattern;public class Test { public static void main(String[] args) { String str ="${time}"; String req="\\$\\{([a-z]{4})\\}"; Pattern pattern=Pattern.compile(req); Matcher m=pattern.matcher(str); boolean s=m.find(); if (s) { String c = m.group(1); System.out.println(c); } else { System.out.println("Not found."); } }}