有一字符串如下::
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();
}
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();
}
解决方案 »
- 调用第三方程序,输出一个txt文件,如何知道是不是第三方程序输出的?
- 求解
- 几个填空题,概念,求救!!!
- JAVA如何开发PDF阅读器
- 关于一段多线程的代码,请教大家。。。。
- 为什么在类中这样声名赋值不行?
- java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
- 做了一个程序是将某个网页上的图片下载下来,然后存到数据库中,下载这个图片是不是用urlconnection就行了??
- App Servers处理send.Redirect("**.jsp")和 <jsp:forward page="**.jsp"/>时有什么不同
- 如何打开一个新窗口?
- 汉字拆分问题
- Swing界面组件过多导致移动界面时死机??
好像半年前我就看到过你,你就是四个三解.半年后还是四个角.
^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^
Matcher m = p.matcher(str);
while (m.find())
System.out.print(m.group(1));
System.out.println();
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));
thomas_20(执子之手,与子偕老)
好像半年前我就看到过你,你就是四个三解.半年后还是四个角.
^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^
----------------------------------------------------------------------
楼主这是什么意思,歧视我吗,有项目来的时候还会有时间上csdn吗
再说,就算我回答了,对了,人家不揭贴不给分又能怎么样啊,人家阎宏博士也都在穿三角呢(半年前是这样的,现在不知道),当然我不能和他比。上csdn是来找答案的,不是升星的,要不我开几个马甲早就....
------------------------------------------------------------------------
楼上的为什么要加?呢?
不太明白呀.
这好象叫懒式匹配
别误会。没有任何其它的意思。
和你开个玩笑。:)
我这个贴的分都给你。
主题: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没找到匹配的串。