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

正则表达式匹配文本
<span class="fb">作者:<a 
                                                                                                                                                href="space.php?action=viewpro&amp;uid=6202438"
                                                                         
                                                                        target="_blank">xwzylpzb</a>

怎么写正则表达式,捕获xwzylpzb?
正则表达式

------解决方案--------------------
public static void main(String[] args) {
String str="<a href=\"space.php?action=viewpro&uid=6202438\" target=\"_blank\">xwzylpzb</a>";
Pattern pattern=Pattern.compile("<a\\s+href=(?<url>.+?)>(?<content>.+?)</a>");
Matcher matcher = pattern.matcher(str);
while(matcher.find()){
System.out.println(matcher.group(2));
}
}

------解决方案--------------------
楼主,匹配“作者”的正则表达式: <span class="fb">(.*):<a
      匹配“xwzylpzb”的正则表达式:   target="_blank">(.*)</a>
因为在整个源文本中,
<span class="fb">作者:<a                 这一段是唯一的。

target="_blank">xwzylpzb</a>              这一段也是唯一的。