当前位置: 代码迷 >> J2EE >> 解析这么一个字符串的方法
  详细解决方案

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

热度:69   发布时间:2016-04-22 00:50:36.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));        }    }
  相关解决方案