我有这段小代码
String[] words = {"{apf","hum_","dkoe","12f"};
for(String s:words)
{
if(s.matches("[a-z]"))
{
System.out.println(s);
}
}
Run Code Online (Sandbox Code Playgroud)
打算打印
dkoe
Run Code Online (Sandbox Code Playgroud)
但它什么都没打印!!
我试图做一个快速的理智检查......它失败了.这是我的代码 -
import java.util.regex.*;
public class Tester {
public static void main(String[] args) {
String s = "a";
Pattern p = Pattern.compile("^(a)$");
Matcher m = p.matcher(s);
System.out.println("group 1: " +m.group(1));
}
}
Run Code Online (Sandbox Code Playgroud)
而我期望的是看到group 1: a.但相反,我得到了一个IllegalStateException: no match found,我不明白为什么.
编辑:我也尝试打印groupCount(),它说有1.