花花括号之间的Java正则表达式匹配 [英] Java Regex matching between curly braces

查看:226
本文介绍了花花括号之间的Java正则表达式匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要解析日志文件并获取时间和相关函数调用字符串
这样存储在日志文件中:{time:2012-09-24T03:08:50, message:Call()started}

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" : "Call() started"}

在其他字符串字符之间会有多个记录时间函数调用,因此我希望使用正则表达式去通过文件并抓住所有这些

There will be multiple logged time function calls in between other string characters, so hence I am hoping to use regex to go through the file and grab all of these

我想获取整个记录的信息,包括大括号

I would like to grab the entire logged information including the curly brackets

我尝试了以下

Pattern logEntry = Pattern.compile("{(.*?)}");
Matcher matchPattern = logEntry.matcher(file);

Pattern.compile("{[^{}]*}");
Matcher matchPattern = logEntry.matcher(file);

我一直收到非法重复错误,请帮忙!谢谢。

I keep getting illegal repetition errors, please help! Thanks.

推荐答案

你需要逃避'{'& '}'带'\'

you need to escape '{' & '}' with a '\'

所以:{(。*?)}变为: \\ {(。*?)\\}

你必须逃离的地方'\'与另一个'\'第一个

where you have to escape the '\' with another '\' first

请参阅: http://www.regular-expressions.info/reference.html 获取需要转义的完整字符列表...

see: http://www.regular-expressions.info/reference.html for a comprehensive list of characters that need escaping...

这篇关于花花括号之间的Java正则表达式匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆