Java Regex matching between curly braces

后端 未结 5 722
说谎
说谎 2020-12-03 03:27

I need to parse a log file and get the times and associated function call string This is stored in the log file as so: {\"time\" : \"2012-09-24T03:08:50\", \"message\" : \"C

5条回答
  •  情歌与酒
    2020-12-03 04:19

    Braces are special regex characters used for repetition groups, therefore you must escape them.

    Pattern logEntry = Pattern.compile("\\{(.*?)\\}");
    

    Simple tester:

     public static void main(String[] args) throws Exception {
            String x =  "{\"time\" : \"2012-09-24T03:08:50\", \"message\" : \"Call() started\"}";
            Pattern logEntry = Pattern.compile("\\{(.*?)\\}");
            Matcher matchPattern = logEntry.matcher(x);
    
            while(matchPattern.find()) {
                System.out.println(matchPattern.group(1));
            }
    
        }
    

    Gives me:

    "time" : "2012-09-24T03:08:50", "message" : "Call() started"
    

提交回复
热议问题