Java regex for nested pattern

Hello regex experts! Here is my riddle:

String input = "aaa${text1${text2}bbb}ccc${text3}";
String regex = "helpMeFindIt";
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(input);
while (m.find()){
   System.out.println(input.substring(m.start(), m.end()));            
}

Please help me find regex that results in the following output:
${text1${text2}bbb}
${text2}
${text3}

P.S. the closest I was able to get is "\\$\\{.+?\\}". Predictably, it provides a wrong answer.
javacubeAsked:
Who is Participating?
 
objectsConnect With a Mentor Commented:
as i said above you cannot use a single regex to locate nested patterns, you instead need to use a second regex on the matched string.
0
 
javacubeAuthor Commented:
Thanks and good luck everyone!
0
 
objectsCommented:
i don't think you can achieve that with a single regex.
the second match will start at "ccc.."
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
javacubeAuthor Commented:
> the second match will start at "ccc.."
with what regex?
0
 
objectsCommented:
with any regex that returns the desired pattern (${text1${text2}bbb}).
I think you need to use a second regex on the matched string, or something like that.
0
 
javacubeAuthor Commented:
could you provide with ANY regex that results in the output starting at "ccc..."
0
 
ozymandiasConnect With a Mentor Commented:
Here :

import java.util.regex.*;

public class regextest3{

      public static void main(String[] args){

            String input = "aaa${text1${text2}bbb}ccc${text3}";
            String regex = "[^\\$]*(.*(\\$\\{[^\\}]*\\})[^\\}]*\\}).*(\\$\\{.*\\})$";
            Pattern p = Pattern.compile(regex);
            Matcher m = p.matcher(input);

            if (m.matches()){
                  System.out.println(m.group(1));
                  System.out.println(m.group(2));
                  System.out.println(m.group(3));
            }else{
                  System.out.println("no match");
            }


      }

}
0
 
javacubeAuthor Commented:
ok, this regex fails in finding the pattern. it matches the whole input string and prints out groups (stuff in brackets).  to test your solution: iterate with m.find, as in the original posting
0
 
ozymandiasCommented:
what do you mean? the output of the program I posted is exactly as described in your question. it matches the pattern and identifies the nested sub-patterns.
0
 
javacubeAuthor Commented:
I checked your regex against my original posting: m.find() with your regex outputs "aaa${text1${text2}bbb}ccc${text3}". This is the only pattern found. It's different from the expected outcome. I believe this is because you mistake groups with pattern matches. They are not equal. The groups are not found - they are programmed with brakets. Groups are hardwired - for example, in your case, group print out results in the desired output if and only if the first group has a nested pattern and is followed by a group without a nested pattern. Just try to add a nested pattern to the second groups, and it breaks, or add a third group - it fails again.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.