首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2EE开发 >

解析这么一个字符串的方法

2012-09-02 
解析这样一个字符串的方法{A0:2011-10-11 12:08:06:This is the first message,A1:2011-10-11 12:0

解析这样一个字符串的方法
{"A0":"2011-10-11 12:08:06:This is the first message","A1":"2011-10-11 12:08:06:This is the second message"}


我只要得到2011-10-11 12:08:06这样的时间和后面This is the first message消息就可以了,前面可能是A0到A1000。

最开始我想用splite按逗号分出A0和A1的消息内容后再通过“:”符分割出时间和消息,但发现时间里面有“:”符所以作罢。

请问大家有相应的解决经验么?

[解决办法]

Java code
    public static void main(String[] args) {        String testStr = "{\"A0\":\"2011-10-11 12:08:06:This is the first message\",\"A1\":\"2011-10-11 12:08:06:This is the second message\"}";        String reg = "\"A[\\d]+\":\"([\\d]{4}-[\\d]{2}-[\\d]{2} [\\d]{2}:[\\d]{2}:[\\d]{2}):([^\"]+)\"";        Pattern p = Pattern.compile(reg);        Matcher matcher = p.matcher(testStr);        while (matcher.find()) {            System.out.println(matcher.group(1) + "   " + matcher.group(2));        }    } 

热点排行