import java.util.regex.*;
class  fjs{
public static void main(String [] args){
String s= "cds";
Pattern pa = Pattern.compile("[^a][^b]*");
Matcher ma = pa.matcher(s);
if(ma.matches()){
System.out.println(ma.group());
}
}
}

解决方案 »

  1.   

    都看点SDK帮助,F1一下什么都有了,“[^a]*”表示只要除了第一个字母不是a都可以,
    "[^a][^b]*"表示第一个不能是a且第二个不能是b,就是你说的这种情况。
      

  2.   

    import java.util.regex.*;
    class  fjs{
    public static void main(String [] args){
    String s= "cds"; ==》String s = "cabds";
    Pattern pa = Pattern.compile("[^a][^b]*");
    Matcher ma = pa.matcher(s);
    if(ma.matches()){
    System.out.println(ma.group());
    }
    }
    }
    就不行了。我的意思是不能包含ab,不是第一是a出现。实际上a在字符串中,可以随便出现啊。
      

  3.   

    你的什么意思? 
    String s = "cabds";结果也是对的呀.
    没有打印出东西,意思就是没有匹配呀.