日期:2014-05-20  浏览次数:20727 次

java正则表达式group(int)问题
本人菜菜鸟,各种不会。
对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://开头的内容。
求各位大哥帮忙,小弟感激不尽!
正则表达式 java group(int)

------解决方案--------------------
你最终想得到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);

------解决方案--------------------
top top top