有一字符串如下::
String str = "3978c'32-1=2\dkjj<group> k另一方面</group>sadfdslkjf<group>sajkewio另一方面</grou>sdoujsdfj<group>sdfkljaskjl;df</group>";我想得到<group></group>中的内容.因为有三个.但我每次得到都是第一次出现<group>的位置一直到最后.下面是我写的
        Pattern p = Pattern.compile("<group>.*</group>");
        Matcher m = p.matcher(str);
        
        while (m.find())
            System.out.print(m.group());
         System.out.println();
    }

解决方案 »

  1.   

    Pattern p = Pattern.compile("<group>.*?</group>");
      

  2.   

    thomas_20(执子之手,与子偕老) 
    好像半年前我就看到过你,你就是四个三解.半年后还是四个角.
    ^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^
      

  3.   

    Pattern p = Pattern.compile("<group>(.*)</group>");
            Matcher m = p.matcher(str);
            
            while (m.find())
                System.out.print(m.group(1));
             System.out.println();
      
      

  4.   

    Pattern p = Pattern.compile("<group>([a-zA-Z0-9 ;\u4e00-\u9fa5]{1,})</group>");
    Matcher m = p.matcher("3978c'32-1=2\\dkjj<group> k另一方面</group>sadfdslkjf<group>sajkewio另一方面</group>sdoujsdfj<group>sdfkljaskjl;df</group>");
    while (m.find())
    System.out.println(m.group(1));
      

  5.   

    回复人: skycncomp(风无行)
    thomas_20(执子之手,与子偕老) 
    好像半年前我就看到过你,你就是四个三解.半年后还是四个角.
    ^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^
    ----------------------------------------------------------------------
    楼主这是什么意思,歧视我吗,有项目来的时候还会有时间上csdn吗
    再说,就算我回答了,对了,人家不揭贴不给分又能怎么样啊,人家阎宏博士也都在穿三角呢(半年前是这样的,现在不知道),当然我不能和他比。上csdn是来找答案的,不是升星的,要不我开几个马甲早就....
    ------------------------------------------------------------------------
    楼上的为什么要加?呢?
    不太明白呀.
    这好象叫懒式匹配
      

  6.   

    thomas_20(执子之手,与子偕老)
    别误会。没有任何其它的意思。
    和你开个玩笑。:)
    我这个贴的分都给你。
      

  7.   

    转自网易 jsp 社区
    主题:java1.4正则表达式的三种匹配模式 
    发信人: gb.lu(白开水@胡涂虫)
    整理人: gb.lu(2003-01-29 14:58:31), 站内信件  
    pattern中三种匹配模式概念:  1  Greedy quantifiers(贪婪模式,偶自己瞎翻的)  
        语法:  
            X?      X, once or not at all   
            X*      X, zero or more times   
            X+      X, one or more times   
            X{n}    X, exactly n times   
            X(n,}   X, at least n times   
            X{n,m}  X, at least n but not more than m times       尽可能多的匹配,例如:  
            pattern = "a.*b";  
            source = "aaabbb";  
            因为aaab是满足条件的,aaabbb也是满足条件的,这种模式就找到尽可能  
            多的并满足条件的串,所以第一次找到的就是整个串aaabbb  2  Reluctant quantifiers(勉强模式)      语法:  
            X??     X, once or not at all   
            X*?     X, zero or more times   
            X+?     X, one or more times   
            X{n}?   X, exactly n times   
            X(n,}?  X, at least n times   
            X{n,m}? X, at least n but not more than m times   
           
        尽可能少的匹配,例如:  
            pattern = "a.*?b";  
            source = "aaabbb";  
            由于aaab首先满足条件,所以返回它,而不管后面如何  3  Possessive quantifiers(占有模式)  
        语法:  
            X?+     X, once or not at all   
            X*+     X, zero or more times   
            X++     X, one or more times   
            X{n}+   X, exactly n times   
            X(n,}+  X, at least n times   
            X{n,m}+ X, at least n but not more than m times   
           
         尽可能多的搜索,不管是否区配,例如:  
            pattern = "a.*+b";  
            source = "aaabbb";  
            由于.*匹配任意多个所有字符,所以.*就一直找下去,aabbb都是,一直到  
           结束(或行尾),结果a.*+b没找到匹配的串。