日期:2014-05-19  浏览次数:20741 次

解析这样一个字符串的方法
{"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));
        }
    }