Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1003197
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T07:56:34+00:00 2026-05-16T07:56:34+00:00

{ Main Block { Nested Block } } { Main Block { Nested Block

  • 0
{
Main Block
     {
     Nested Block
     }
}
{
Main Block 
     {
     Nested Block
     }
     {
     Nested Block
     }
}

I want to get data within Main Blocks including its Nested Blocks with Java Regex. Is it possible?

Thanks in Advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T07:56:35+00:00Added an answer on May 16, 2026 at 7:56 am

    IF there can only be at most 1 level of nesting, and the braces characters can not be escaped, then in fact the regex pattern for this is quite simple.

    Essentially the structure we have, in some abstract notation, is:

    {…(?:{…}…)*…}
    

    Here’s a visual breakdown:

      ___top___
     /   nest  \
    /    / \    \
    {…(?:{…}…)*…}
    | \______/| |
    |         | |
    open      | close
              |
         zero or more
    

    This is not quite regex, of course, because:

    • In “real” regex, we must escape the { and }, since they’re metacharacters
    • In “real” regex, we need to replace … with the actual pattern for content
      • [^{}]*+ would be a fine pattern. The […] is a character class. [^…] is a negated character class. The * is zero-or-more repetition. The + following the repetition specifier is the possessive quantifier.

    So, meta-regexing technique is used to programmatically transform this abstract pattern (which is readable) to valid regex pattern (which can be ugly at times like this). Here’s an example (also see on ideone.com):

        import java.util.*;
        import java.util.regex.*;
        //...
    
        Pattern block = Pattern.compile(
            "{…(?:{…}…)*…}"
                .replaceAll("[{}]", "\\\\$0")
                .replace("…", "[^{}]*+")
        );
        System.out.println(block.pattern());
        // \{[^{}]*+(?:\{[^{}]*+\}[^{}]*+)*[^{}]*+\}
    
        String text
            = "{ main1 { sub1a } { sub1b } { sub1c } }\n"
            + "{ main2\n"
            + "   { sub2a }\n"
            + "       { sub2c }\n"
            + "}"
            + "   { last one, promise }    ";
    
        Matcher m = block.matcher(text);
        while (m.find()) {
            System.out.printf(">>> %s <<<%n", m.group());
        }
        // >>> { main1 { sub1a } { sub1b } { sub1c } } <<<
        // >>> { main2
        //    { sub2a }
        //        { sub2c }
        // } <<<
        // >>> { last one, promise } <<<        
    

    As you can see, the actual regex pattern is therefore:

    \{[^{}]*+(?:\{[^{}]*+\}[^{}]*+)*[^{}]*+\}
    

    Which as a Java string literal:

    "\\{[^{}]*+(?:\\{[^{}]*+\\}[^{}]*+)*[^{}]*+\\}"
    

    Variations

    If the nesting level can be deeper, then regex can still be used. You can also allow the { and } to be “escaped” (i.e. used in the content part but not as block delimiter).

    The final regex pattern will be quite complicated, but depending on how comfortable you are with meta-regexing (which requires you to be comfortable with regex itself), the code can be quite readable and manageable.

    If the nesting level can be arbitrarily deep, then some flavors (e.g. .NET or Perl) can still handle it, but Java regex is not powerful enough to handle it.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am just wondering... How to block main thread as JOptionPane (its confirmation dialog)
I want to add the active class to all the main category block in
I want to implement a language construct similiar to Ruby´s blocks. So main() {
When i have a code block static void Main() { foreach (int i in
I have stylesheet block in base layout: {% stylesheets filter='cssrewrite' 'bundles/static/css/main.css' %} <link rel=stylesheet
I have a JS code like this <script type=text/javascript> $(document).ready(function(){ $('.main').on('click', '.block', function (){
http://jsfiddle.net/nUcXQ/ How to get dynamic height on the main (black background) I'm using image
I have my main stage, and I have two objects (blocks), these two objects
Basically, I have a main container div with nested content. This div is has
I have a string with nested groups like this ('blabla' is some text within

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.