关于捕获组的java正则表达式问题

时间:2016-03-10 08:46:52

标签: java regex

public void test(){
    String source = "hello<a>goodA</a>boys can <a href=\"www.baidu.com\">goodB</a>\"\n"
                + "                + \"this can help";
    Pattern pattern = Pattern.compile("<a[\\s+.*?>|>](.*?)</a>");
    Matcher matcher = pattern.matcher(source);
    while (matcher.find()){
        System.out.println("laozhu:" + matcher.group(1));
    }
}

输出:

laozhu:goodA
laozhu:href="www.baidu.com">goodB

为什么第二场比赛不是laozhu:goodB

2 个答案:

答案 0 :(得分:1)

试试这个正则表达式:

<a(?: .*?)?>(\w+)<\/a>

所以你的Pattern应该是这样的:

Pattern pattern = Pattern.compile("<a(?: .*?)?>(\\w+)<\\/a>");

匹配goodAgoodB

有关详细说明,请查看此处:Regex101

答案 1 :(得分:1)

    Pattern pattern = Pattern.compile("<a.*?>(.*?)</a>");