比如下面几行网页源代码:
<meta property="fb:admins" content="550301723,1033888255,100000279817523" />
<meta property="og:type" content="article" />
<meta property="og:description" content="Defense secretary Leon Panetta says cyberattacks against critical infrastructure at home and abroad--some of which he called the worst to date--should spark urgent action against the hacker threat." />我想要提取网页中的第二个标签里的一部分内容,og:type ,
如果用正则表达式 (?<=meta property=\").+?(?=\") 的话,这三个标签的“property=”后面跟着的内容都会被匹配到。我只想要第二个的,该怎么做?望高手指教正则表达式java

解决方案 »

  1.   

    我对正则不熟,我想你可不可以在这个正则前面匹配content,在其后匹配property,这样就可以提取中间的内容了吧。意思就是必须有一个在第一个之后,第三个之前的标签,取出其值,不知道可以不...
      

  2.   

    这样行不?String content = 
    "<meta property=\"fb:admins\" " +
    "content=\"550301723,1033888255,100000279817523\" />" +
    "<meta property=\"og:type\" content=\"article\" />" +
    "<meta property=\"og:description\"" +
    " content=\"Defense secretary Leon Panetta says" +
    " cyberattacks against critical infrastructure at " +
    "home and abroad--some of which he called the worst" +
    " to date--should spark urgent action against the hacker threat.\" />";
    Pattern pattern = Pattern.compile("<meta[^/]+?/><meta property=\"([^\"]+)\"");
    Matcher matcher = pattern.matcher(content);
    while (matcher.find()) {
    System.out.println(matcher.group(1));
    }
      

  3.   

    真好,这样就能争取提取了,我不知道我这样的理解是不是对的:
    <meta[^/]+?/>//这是第一次匹配,将所有的meta中的内容匹配出来
    <meta property=\"([^\"]+)\"//这是在第一次匹配基础上的第二次匹配,只匹配property=""双引号里的内容
    但我不明白为什么输出group(1)的时候,会输出第二个,而其他的不会输出,能解释一下吗?谢谢了!
      

  4.   

    <meta[^/]+?/><meta property=\"([^\"]+)\"
    这个匹配的是连续两个meta标签,并把第二个meata标签的property的值用()分组了([^\"]+),所以可以用group(1)取出来