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

Ask A Question

Stats

  • Questions 536k
  • Answers 536k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer First, I assume you mean the cached values may or… May 17, 2026 at 1:31 am
  • Editorial Team
    Editorial Team added an answer Yes for a very large repository, it is recommended that… May 17, 2026 at 1:31 am
  • Editorial Team
    Editorial Team added an answer svnserve is used whenever you access URLs starting with svn://.… May 17, 2026 at 1:31 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I have a string with nested groups like this ('blabla' is some text within
How to get the co-ordinates of the outline shape formed using smaller grid blocks.
I'm making a ul-based horizontal navbar, but I want to have two levels heading
I have been writing several class templates that contain nested iterator classes, for which
If anyone familiar with Rebecca Wirfs-Brock, she has a piece of Java code found
int main (int argc, const char * argv[]) { <br> NSAutoreleasePool * pool =
I was wondering, is it good practice to return from try block? package debug;
How can I create my own methods which take a block as an argument
I'm working on a memory pool for a small game engine. The main use
is there any standard approach which I've missed at school to dump C structure

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.