Solved

Java regex for nested pattern

Posted on 2004-04-29
11
922 Views
Last Modified: 2008-03-06
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.
0
Comment
Question by:javacube
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 2
11 Comments
 

Author Comment

by:javacube
ID: 10956631
Thanks and good luck everyone!
0
 
LVL 92

Expert Comment

by:objects
ID: 10956666
i don't think you can achieve that with a single regex.
the second match will start at "ccc.."
0
 

Author Comment

by:javacube
ID: 10956943
> the second match will start at "ccc.."
with what regex?
0
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
LVL 92

Expert Comment

by:objects
ID: 10956984
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
 

Author Comment

by:javacube
ID: 10957015
could you provide with ANY regex that results in the output starting at "ccc..."
0
 
LVL 15

Assisted Solution

by:ozymandias
ozymandias earned 125 total points
ID: 10957954
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
 

Author Comment

by:javacube
ID: 10962135
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
 
LVL 15

Expert Comment

by:ozymandias
ID: 10962267
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
 

Author Comment

by:javacube
ID: 10963377
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
 
LVL 92

Accepted Solution

by:
objects earned 125 total points
ID: 10964724
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Running JavaFX on JDeveloper 12C 1 108
Bot application - advice 3 79
program arguments vs VM arguments 4 56
import as existing maven project 3 77
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
In this post we will learn different types of Android Layout and some basics of an Android App.
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

737 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question