正则表达式的问题

skycncomp 2005-06-17 01:11:54
有一字符串如下::
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();
}
...全文
186 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
skycncomp 2005-06-18
  • 打赏
  • 举报
回复
thomas_20(执子之手,与子偕老)
别误会。没有任何其它的意思。
和你开个玩笑。:)
我这个贴的分都给你。
haha1903 2005-06-18
  • 打赏
  • 举报
回复
转自网易 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没找到匹配的串。



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

umbrella1984 2005-06-17
  • 打赏
  • 举报
回复
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));
majy 2005-06-17
  • 打赏
  • 举报
回复
Pattern p = Pattern.compile("<group>(.*)</group>");
Matcher m = p.matcher(str);

while (m.find())
System.out.print(m.group(1));
System.out.println();

skycncomp 2005-06-17
  • 打赏
  • 举报
回复
thomas_20(执子之手,与子偕老)
好像半年前我就看到过你,你就是四个三解.半年后还是四个角.
^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^^_^
skycncomp 2005-06-17
  • 打赏
  • 举报
回复
楼上的为什么要加?呢?
不太明白呀.
thomas_20 2005-06-17
  • 打赏
  • 举报
回复
Pattern p = Pattern.compile("<group>.*?</group>");

62,628

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧