日期:2014-05-19  浏览次数:20839 次

【100分求正解】求正则,求恩赐,求解脱
<li class="item"><a onclick="" href="/attraction_review-d19-s64844-05p1-huangshanfengjingqu.html">很好</a><span class="rank-bar"><em style="width: 80px"></em></span><a onclick="" href="/attraction_review-d19-s64844-05p1-huangshanfengjingqu.html">8</a></li><li class="item"><a onclick="" href="/attraction_review-d19-s64844-04p1-huangshanfengjingqu.html">较好</a><span class="rank-bar"><em style="width: 10px"></em></span><a onclick="" href="/attraction_review-d19-s64844-04p1-huangshanfengjingqu.html">1</a></li><li class="item"><span class="fake-a">一般</span><span class="rank-bar"><em style="width: 0px"></em></span><span class="fake-a">0</span></li><li class="item"><a onclick="" href="/attraction_review-d19-s64844-02p1-huangshanfengjingqu.html">较差</a><span class="rank-bar"><em style="width: 10px"></em></span><a onclick="" href="/attraction_review-d19-s64844-02p1-huangshanfengjingqu.html">1</a></li><li class="item"><span class="fake-a">很差</span><span class="rank-bar"><em style="width: 0px"></em></span><span class="fake-a">0</span></li></ul></div></div>

正则分别匹配很好的评价数 8  较好的评论数 1 很差的评论数 0
说明:很好,8,较好,1前面的<a>中的href会变动,最后取值需要group(0)
正则

------解决方案--------------------


public class Test05 {

static String str = "<li class=\"item\"><a onclick=\"\" href=\"/attraction_review-d19-s64844-05p1-huangshanfengjingqu.html\">很好</a><span class=\"rank-bar\"><em style=\"width: 80px\"></em></span><a onclick=\"\" href=\"/attraction_review-d19-s64844-05p1-huangshanfengjingqu.html\">8</a></li><li class=\"item\"><a onclick=\"\" href=\"/attraction_review-d19-s64844-04p1-huangshanfengjingqu.html\">较好</a><span class=\"rank-bar\"><em style=\"width: 10px\"></em></span><a onclick=\"\" href=\"/attraction_review-d19-s64844-04p1-huangshanfengjingqu.html\">1</a></li><li class=\"item\"><span class=\"fake-a\">一般</span><span class=\"rank-bar\"><em style=\"width: 0px\"></em></span><span class=\"fake-a\">0</span></li><li class=\"item\"><a onclick=\"\" href=\"/attraction_review-d19-s64844-02p1-huangshanfengjingqu.html\">较差</a><span class=\"rank-bar\"><em style=\"width: 10px\"></em></span><a onclick=\"\" href=\"/attraction_review-d19-s64844-02p1-huangshanfengjingqu.html\">1</a></li><li class=\"item\"><span class=\"fake-a\">很差</span><span class=\"rank-bar\"><em style=\"width: 0px\"></em></span><span class=\"fake-a\">0</span></li></ul></div></div>";

//[很好 , 较好 , 较差 ] 都用这个方法
static String findA(String key){
String regexp = "<a.*?href=\"(.*?)\".*?>" + key + "</a>.*?<a.*?href=\"(\\1)\".*?>(.*?)</a>";
Matcher m = Pattern.compile(regexp).matcher(str);
if(m.find()){