日期:2014-05-20  浏览次数:20824 次

关于正则表达式
有字符串如下:  
                      <div   class= "midden ">
<div   id= "news ">
<h2> 要闻 </h2>
<ul>
<li   > <a   href= "http://news.qq.com/a/20070805/001221.htm "   class= "rlk "   target= "_blank "> </DIV> </DIV> <li   > <a   href= "http://comic.qq.com/z/hexie/index.htm "   class= "rlk "   target= "_blank ">

请问:如何将   <div   id= "news "> ....... "> </DIV> </DIV> 中的内容取出来。字符串中间是有一些特殊符号的。关键是Pattern不知道怎么写

------解决方案--------------------
String regex= " <div class=\ "midden\ "> [\\s\\w\ "\\*\\&\\-\\+\\/\\[\\] <> ()?,#@$%. '_`=:;|!~]* </DIV> ";
Pattern contentA=Pattern.compile(regex);
这么写应该可以取出.不过分数给的太少了.
------解决方案--------------------
可以这样做。

如果要取 <div id= "news "> 和 </DIV> 之间的内容 可以先把 </DIV> 替换成其他字符 比如换成@

String s = "................... ";//源内容
s = s.replaceAll( " </DIV> ", "@ ");
String reg = " <div id=\ "news\ "> ([^@]*)@ ";
Pattern p = Pattern.compile(reg, Pattern.CASE_INSENSITIVE);
Matcher m = p.matcher(s);
if (m.find()) s = m.group(1);
s就是其中的内容了
------解决方案--------------------

楼上的是看的什么书学的正则? 推荐下
最好是中文的,e文实在不咋地