首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java相关 >

正则表达式婚配文本

2013-04-26 
正则表达式匹配文本span classfb作者:ahrefspace.php?actionviewpro&uid6202438target_b

正则表达式匹配文本
<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>              这一段也是唯一的。

热点排行