我正在尝试使用正则表达式提取一些内容:
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+{((?:[^{]*{[^}]*})*[^}]*)}");
关于java - 用于精确提取 {} 之间的字符串的正则表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31441538/