本人菜菜鸟,各种不会。
对java正则表达式有点头晕,下面这段代码,本人有两个疑问
String astr = "background=\"http://a.jpg,aa background=\"a.jpg,bb background=\"b.jpg\"  ";
 Pattern pattern = Pattern.compile("(background=\"(.*)\")");
 Matcher matcher = pattern.matcher(astr);我用matcher.grountCount()表示匹配到了三次,但是我用matcher.group(2)打印的时候怎么也打印不出第二,三次匹配的内容。问题一,我如何将三次匹配的matcher.group(2)放到字符串数组里面,问题二,我如何不匹配matcher.group(2)中那个http://开头的内容。
求各位大哥帮忙,小弟感激不尽!
正则表达式javagroup(int)

解决方案 »

  1.   

    你最终想得到a.jpg a.jpg b.jpg 吧?
    你的astr是不是有问题?我怎么感觉a.jpg后面应该有个引号?
    String astr = "background=\"http://a.jpg\",aa background=\"a.jpg\",bb background=\"b.jpg\"  ";
    Pattern pattern = Pattern.compile("(background=\"(http://)?(.*?)\")");
    Matcher matcher = pattern.matcher(astr);
    List<String> str = new ArrayList<String>();
    while (matcher.find())
    str.add(matcher.group(3));
    for (String s : str)
    System.out.println(s);
      

  2.   

    嗯,后来我自己也发现了,是因为我的astr写的有问题,害得自己郁闷了很久。谢谢了!
      

  3.   

    关于排除字符串匹配,小弟也找了一篇好文章,转载在自己的博客里。
    http://blog.csdn.net/mingjun_fan/article/details/8547566