简体   繁体   English

大括号之间的Java正则表达式匹配

[英]Java Regex matching between curly braces

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"}我需要解析一个日志文件并获取时间和相关的函数调用字符串 这在日志文件中存储如下: {"time" : "2012-09-24T03:08:50", "message" : "Call( ) 开始"}

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我想获取包括大括号在内的整个记录​​信息

I have tried the following我已经尝试了以下

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

and

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

I keep getting illegal repetition errors, please help!我不断收到非法重复错误,请帮助! Thanks.谢谢。

you need to escape '{' & '}' with a '\\'你需要用 '\\' 转义 '{' & '}'

so: "{(.*?)}" becomes: "\\\\{(.*?)\\\\}"所以: "{(.*?)}"变成: "\\\\{(.*?)\\\\}"

where you have to escape the '\\' with another '\\' first你必须先用另一个 '\\' 转义 '\\'

see: http://www.regular-expressions.info/reference.html for a comprehensive list of characters that need escaping...请参阅: http : //www.regular-expressions.info/reference.html以获取需要转义的字符的完整列表...

Braces are special regex characters used for repetition groups, therefore you must escape them. Braces是用于重复组的特殊正则表达式字符,因此您必须对它们进行转义。

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"

You should use a positive lookahead and lookbehind:您应该使用积极的前瞻和后视:

(?<=\{)([^\}]+)(?=\})
  • (?<={) Matches everything followed by { (?<={) 匹配所有后面跟着 {
  • ([^}]+) Matches any string not containing } ([^}]+) 匹配任何不包含 } 的字符串
  • (?={) Matches everything before { (?={) 匹配 { 之前的所有内容

{} in regexp have special meaning, so they need to be escaped. regexp 中的 {} 有特殊含义,需要转义。

Usually escaping is achieved by preceeding the character to be escaped with a backslash.通常转义是通过在要转义的字符前面加上反斜杠来实现的。 In a character class defined with square brackets, you shouldn't need to do this在用方括号定义的字符类中,您不需要这样做

So something like所以像

Pattern.compile("\{[^{}]*\}");

Could be nearer to what you want to do可能更接近你想做的事

This works perfectly for non-nested brackets but for expressions like这对于非嵌套括号非常适用,但对于像这样的表达式

(sum(x) * 100) / (sum(y) + sum(z))

[az]*[\\{]+([a-zA-Z0-9]+)[\\}]+ works. [az]*[\\{]+([a-zA-Z0-9]+)[\\}]+有效。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM