2014-09-19 71 views
0

下面的代码是否正确如果我打印了matcher.group(0),则发生错误。我是否以正确的方式打印值?Plz help.Logcat错误如下。将正则表达式的匹配器的值打印到logcat

String GetCddata=<p><a href="http://myimagefactorycollection.files.wordpress.com/2014/09/2db83fcf95c5fc036a00abfb412f50e4.jpg"> 
<img class="alignnone size-full wp-image-12" src="http://myimagefactorycollection.files.wordpress.com/2014/09/2db83fcf95c5fc036a00abfb412f50e4.jpg?w=529" alt="2db83fcf95c5fc036a00abfb412f50e4" /> 
</a><a href="https://myimagefactorycollection.files.wordpress.com/2014/09/0e397a47f88e18f8fb91d17db18c7edd-copy.jpg"><img class="alignnone size-full wp-image-4" src="http://myimagefactorycollection.files.wordpress.com/2014/09/0e397a47f88e18f8fb91d17db18c7edd-copy.jpg?w=529" alt="0e397a47f88e18f8fb91d17db18c7edd - Copy" /> 
</a></p><br /> <a rel="nofollow" href="http://feeds.wordpress.com/1.0/gocomments/myimagefactorycollection.wordpress.com/3/"><img alt="" border="0" src="http://feeds.wordpress.com/1.0/comments/myimagefactorycollection.wordpress.com/3/" /> 
</a> <img alt="" border="0" src="http://pixel.wp.com/b.gif?host=myimagefactorycollection.wordpress.com&#038;blog=75018866&#038;post=3&#038;subd=myimagefactorycollection&#038;ref=&#038;feed=1" width="1" height="1" /> 
    ]]; 

Pattern pattern = Pattern.compile("(?<=\\<a href=)(.*?)\\>"); 
Matcher matcher = pattern.matcher(GetCddata); 
Log.v("dd",matcher.group(0)); 

http://i.stack.imgur.com/GKpOD.png

+0

您得到一个错误:非法状态异常没有匹配成功,到目前为止 – 2014-09-19 08:08:55

回答

0

组:

public String group() 

返回由以前匹配所匹配的输入子序列。 对于输入序列为s的匹配器m,表达式m.group()和s.substring(m.start(),m.end())是等价的。

请注意,某些模式(例如a *)与空字符串匹配。当模式成功匹配输入中的空字符串时,此方法将返回空字符串。

指定者: 组接口MatchResult的 返回: (可能是空)子由以前匹配所匹配,以字符串形式

抛出: IllegalStateException - 如果没有匹配尚未尝试,或者以前的匹配操作失败

来源:docs.oracle.com

Sugg estion:检查你的正则表达式,附上您Log.v();try...catch

+0

感谢ü这么多.. – 2014-09-19 08:19:16

+1

如果有帮助,然后接受它。 – 2014-09-19 08:39:20

+0

接受答案,如果它适合你...如果可能的话投票。快乐编码:) – 2014-09-19 10:25:55