public static void main(String arg[]) throws Exception {
String s = "String str=\"adfaa21aabb/dfccdsfdsds23ccdd/oiuodfg2ggee/";
Pattern p = Pattern.compile("df(.*?)/", Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(s);
while (m.find()) {
System.out.println(m.group(1));
}
}

解决方案 »

  1.   

    我是这样写的,但是只截取了第一段,我的思路是想截取完第一段之后就用一个新的字符串代替之前的字符串,新的字符串是没有了刚刚截取过得。但是下面的代码还是出现了问题。
    public static void main(String args[]){
    String str="adfaa21aabb/dfccdsfdsds23ccdd/oiuodfg2ggee/";
    String s1 ="";
    int i1 =0;
    int i2 =0;
    while((i1=str.indexOf("df",i1))!=-1)
    {
    i1=str.indexOf("df",i1);
    i2=str.indexOf("/",i2);
    if(i1<i2){
    s1=str.substring(i1, i2+1);
    System.out.println(s1);
    }
    str=str.substring(i1,i2+1);

    }
    }
      

  2.   

    /*
     * 根据开头的字符结尾的字符以及之间的字符返回匹配的字符串
     * str 待匹配的字符串,begin开始匹配的字符串,key匹配的中间字符,end匹配的结束字符
     */
    public static String[] match(String str, String begin,String key,String end)
    {
        String [] strMc = new String[9]; 
        int beginIndex = str.indexOf(begin);
        int endLength = end.length();
        int endIndex = str.indexOf(end,beginIndex+begin.length())+endLength;
        int i = 0;
        while(-1!=beginIndex&&-1!=endIndex)
        {
            String sub = str.substring(beginIndex, endIndex);
             if(-1!=sub.indexOf(key))
                {
                 strMc[i] = new String(str.substring(beginIndex, endIndex+endLength));
                 i++;
                }
             beginIndex = str.indexOf(begin, endIndex);
                endIndex = str.indexOf(end, beginIndex);
        }
        
        return strMc;
    }
    可以match("df","","/");
      

  3.   

    String str = "adfaa21aabb/dfccdsfdsds23ccdd/oiuodfg2ggee/";
    String[] strs=str.split("/");
    for (String s : strs) {
    if(s.indexOf("df")!=-1){
    System.out.println(s.substring(s.indexOf("df")+2,s.length()));
    }
    }