寻一正则表达式匹配<meta name="description" content="....../>

解决方案 »

  1.   

    String regex="<meta[^<>]*/>";
      

  2.   

    你这个是匹配<meta开头的吧  我要的是<meta name="description" content="....../>然后获取content的内容的
      

  3.   

    你只是说匹配<meta name="description" content="....../>又没说匹配content中的内容
      

  4.   


    String str = "<meta name=\"description\" content=\"内容\"/>";
    Matcher m=Pattern.compile("<meta.*content=\"([^<>]*)\"/>").matcher(str);
    if(m.find())
    System.out.println(m.group(1));
      

  5.   


    String str = "<meta name=\"description\" content=\"内容1\"/><meta name=\"description\" content=\"内容2\"/>";
            Matcher m=Pattern.compile("<meta.*?content=\"([^<>]*)\"/>").matcher(str);//这里没贪婪限制的话会丢失掉前面的 内容1
            while(m.find())
                System.out.println(m.group(1));
      

  6.   


    String str = "<meta name=\"description\" content=\"内容1\"/><meta name=\"description\" content=\"内容2\"/>";
            Matcher m=Pattern.compile("<meta[^<>]*content=\"([^<>]*)\"/>").matcher(str);
            while(m.find())
                System.out.println(m.group(1));
      

  7.   


    很专业,能直接提取匹配的值,又让我学到了不少东西,但是楼主明显是要从网页上抓取name="description" 里面的内容,现在用这个代码把所有<meta>里content的内容都提取出来了,所以不如把name限制加进去Matcher m=Pattern.compile("<meta name=\"description\"[^<>]*content=\"([^<>]*)\"/>").matcher(str);如果在想把这个写的完美就会发现有的网站并不是meta后面就跟着name,比如这个界面就是“<meta content="寻一正则表达式匹配 meta name description content" name="description"/>”这样的话这个正则又失去效果了,可是要是把name限制加进去又有点不好写这个正则,毕竟还要判断name和content的位置,所以最好的解决方法就是在提取某个网页时首先在正则表达式里指定name的位置,在同一网站的不同级网页中大多都是一致的