小白java正则表达式遇到问题,求助啊。
本帖最后由 prog_C 于 2013-04-24 09:23:42 编辑 想解析人人好友的相关信息,特意学了一段时间的正则表达式,但还是遇到很着急的问题,求帮忙看一下:
final String friendList = "var friends=[{"id":422727123,"vip":false,"selected":true,"mo":false,"name":"\u9648\u671b\u6797","head":"http:\\/\\/hdn.xnimg.cn\\/photos\\/hdn121\\/20111020\\/2120\\/tiny_dfoU_59555d019117.jpg","groups":[]},{"id":123456789,"vip":false,"selected":false,"mo":false,"name":"\u9648\u76ca\u714c","head":"http:\\/\\/hdn.xnimg.cn\\/photos\\/hdn121\\/20130423\\/0105\\/tiny_Vhoj_f8eb00022ee4111a.jpg","groups":[]}];";
final String regex = ""id":(\\d{9}),"vip":.*"name":"(.*)","head":"(.*.jpg)","";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(friendList);
int i;
while(matcher.find())
{
System.out.println("GroupCount : "+ matcher.groupCount());
System.out.println("Start : "+matcher.start());
System.out.println("End : "+matcher.end());
for(i = 0;i<=matcher.groupCount();i++)
{
System.out.println(matcher.group(i));
}
}
final String friendList = "var friends=[{"id":422727123,"vip":false,"selected":true,"mo":false,"name":"\u9648\u671b\u6797","head":"http:\\/\\/hdn.xnimg.cn\\/photos\\/hdn121\\/20111020\\/2120\\/tiny_dfoU_59555d019117.jpg","groups":[]},{"id":123456789,"vip":false,"selected":false,"mo":false,"name":"\u9648\u76ca\u714c","head":"http:\\/\\/hdn.xnimg.cn\\/photos\\/hdn121\\/20130423\\/0105\\/tiny_Vhoj_f8eb00022ee4111a.jpg","groups":[]}];";
final String regex = ""id":(\\d{9}),"vip":.*?"name":"(.*?)","head":"(.*?.jpg)","";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(friendList);
int i;
while(matcher.find())
{
System.out.println("GroupCount : "+ matcher.groupCount());
System.out.println("Start : "+matcher.start());
System.out.println("End : "+matcher.end());
for(i = 0;i<=matcher.groupCount();i++)
{
System.out.println(matcher.group(i));
}
}