java - 用于精确提取 {} 之间的字符串的正则表达式

标签 java regex

我正在尝试使用正则表达式提取一些内容:

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

对于文本 block :

{ /*uninterested in this*/ 
"
digraph Checker 
{ 
/*bunch of stuff*/
{
/*bunch of stuff*/
}
{
/*bunch of stuff*/
}
{
/*bunch of stuff*/
}
/*bunch of stuff*/
} //first most curly brace ends, would want the regex to filter out till here, incl. the braces
"
}

并期望输出为:

digraph Checker 
{ 
/*bunch of stuff*/
{
/*bunch of stuff*/
}
{
/*bunch of stuff*/
}
{
/*bunch of stuff*/
}
/*bunch of stuff*/
}

但似乎无法摆脱最后一个

"
}

有什么办法可以提取这个吗?

最佳答案

您可以使用此正则表达式:

Pattern logEntry = Pattern.compile("digraph Checker\\s+{((?:[^{]*{[^}]*})*[^}]*)}");

RegEx Demo

关于java - 用于精确提取 {} 之间的字符串的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31441538/

相关文章:

java - 关闭 DAO 层中 jdbc 中结果集的连接

java - Mockito:如何验证来自另一个单例类的单例类的方法调用

php - 从条件 REGEX 获取捕获组

regex - 句号通配符在 Bash 模式替换中不起作用

python - python使用正则表达式去除括号中的内容

javascript - 如何删除字符串中重复的空格?

java - 如何使用FFMPEG将视频文件转换为音频文件?

java - 从列表 : old approach vs stream design thought? 中删除重复项

java.io.IOException : Cannot run program "scala": CreateProcess error=2,

regex - 正则表达式考虑字符串中的特殊字符