program A {
int x = 10;
tuple date {
int day;
int month;
int year;
}
}
function B {
int y = 20;
...
}
process C {
more code;
}
I’d like to extract whatever is inside the outer curly braces for program, function and process. In terms of the output, I want to see three matches:
int x = 10;
tuple date {
int day;
int month;
int year;
} //first match
int y = 20;
... //second match
more code; //third match
I’ve achieved this using Javascript. The regular expression I use is /(program|function|process).*?{(.*?)}\n+(program|function|process)/m, which works as demonstrated by Rubular.
However, when I use the same expression in Java it wouldn’t work any more. It only returns the first match. I have a vague memory that the consumed text in a previous match will not be matched again. In my case, the keywords program and function have been consumed in the first match, resulting in no further matches. Is there a way in Java to match the consumed text?
Edit: the Java code is posted below as requested.
public class Test {
public static void main(String[] args) throws IOException {
String input = FileUtils.readFileToString(new File("input.txt"));
Pattern p = Pattern.compile("(program|function|process)[^\\{]*?\\{(.*?)\\}\\s*(program|function|process)", Pattern.DOTALL);
Matcher m = p.matcher(input);
while(m.find()) {
System.out.println(m.group(2));
}
}
}
You can solve your problem by using lookarounds..so your regex would be
Group 1 would have your data..