You can solve your problem in inverse mode :
public String getResult(String text) {
Pattern pattern = Pattern.compile("(\\{.*)\\}");
Matcher matcher = pattern.matcher(text);
if (matcher.find()) {
return matcher.group(1);
} else {
return null;
}
}
public static void main(String args[]) {
Mcve mcve = new Mcve();
String text = "{HHH { AAAA } fS } SFS}";
while ((text = mcve.getResult(text)) != null) {
System.out.println(text);
}
}
Output
{HHH { AAAA } fS } SFS
{HHH { AAAA } fS
{HHH { AAAA
The idea is to get the first result then send this result to the same method until the method return null
for example :
- Send this String
{HHH { AAAA } fS } SFS}
receive {HHH { AAAA } fS } SFS
- Send the result of the previous Iteration
{HHH { AAAA } fS } SFS
receive {HHH { AAAA } fS
- Send the result of the previous Iteration
{HHH { AAAA } fS
receive {HHH { AAAA
- Send the result of the previous Iteration
{HHH { AAAA
receive null
now STOP