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

  • Home
  • SEARCH
  • 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 123207
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T04:15:00+00:00 2026-05-11T04:15:00+00:00

I have a text string structured like this: = Some Heading (1) Some text

  • 0

I have a text string structured like this:

= Some Heading (1)  Some text  == Some Sub-Heading (2)  Some more text  === Some Sub-sub-heading (3)  Some details here  = Some other Heading (4) 

I want to extract the content of second heading, including any subsection. I do not know beforehand what is the depth of the second heading, so I need to match from there to the next heading that is of the same depth, or shallower, or the end of the string.

In the example above, this would yield:

== Some Sub-Heading (2)  Some more text  === Some Sub-sub-heading (3)  Some details here 

This is where I get stuck. How can I use the matched sub-expression opening the second heading as part of the sub-expression for closing the section.

  • 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. 2026-05-11T04:15:01+00:00Added an answer on May 11, 2026 at 4:15 am

    I’d skip trying to use a complex regex. Instead write a simple parser and build up a tree.

    Here’s a rough and ready implementation. It’s only optimized for lazy coding. You may want to use libraries from CPAN to build your parser and your tree nodes.

    #!/usr/bin/perl  use strict; use warnings;  my $document = Node->new(); my $current = $document;  while ( my $line = <DATA> ) {      if ( $line =~ /^=+\s/ ) {          my $current_depth = $current->depth;         my $line_depth = Node->Heading_Depth( $line );          if ( $line_depth > $current_depth ) {             # child node.             my $line_node = Node->new();             $line_node->heading( $line );             $line_node->parent( $current );             $current->add_children( $line_node );             $current = $line_node;         }         else {              my $line_node = Node->new();             while ( my $parent = $current->parent ) {                  if ( $line_depth == $current_depth ) {                     # sibling node.                     $line_node->heading( $line );                     $line_node->parent( $parent );                     $current = $line_node;                     $parent->add_children( $current );                      last;                 }                  # step up one level.                 $current = $parent;             }         }      }     else {         $current->add_children( $line );     }   }  use Data::Dumper; print Dumper $document;  BEGIN {     package Node;     use Scalar::Util qw(weaken blessed );      sub new {         my $class = shift;          my $self = {             children => [],             parent   => undef,             heading  => undef,         };          bless $self, $class;     }      sub heading {         my $self = shift;         if ( @_ ) {             $self->{heading} = shift;         }         return $self->{heading};     }      sub depth {         my $self = shift;          return $self->Heading_Depth( $self->heading );     }      sub parent {         my $self = shift;         if ( @_ ) {             $self->{parent} = shift;             weaken $self->{parent};         }         return $self->{parent};     }      sub children {         my $self = shift;         return @{ $self->{children} || [] };     }      sub add_children {         my $self = shift;         push @{$self->{children}}, @_;     }      sub stringify {         my $self = shift;          my $text = $self->heading;         foreach my $child ( $self->children ) {             no warnings 'uninitialized';             $text .= blessed($child) ? $child->stringify : $child;         }          return $text;     }      sub Heading_Depth {         my $class  = shift;         my $heading = shift || '';          $heading =~ /^(=*)/;         my $depth = length $1;           return $depth;     }  }  __DATA__ = Heading (1)  Some text  = Heading (2)  Some more text  == Subheading (3)  Some details here  == Subheading (3)  Some details here  = Heading (4) 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

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

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

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

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

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer CREATE TABLE [dbo].[myTable] ( [a] [int] NULL, [b] [varchar](50) NULL,… May 12, 2026 at 6:02 pm
  • Editorial Team
    Editorial Team added an answer Yes, you definitely should add the index. CREATE INDEX Post_author_id… May 12, 2026 at 6:02 pm
  • Editorial Team
    Editorial Team added an answer Ah - you can define them like this: <attribute name="Server">192.168.0.1,192.168.0.2,192.168.0.3</attribute> May 12, 2026 at 6:02 pm

Related Questions

Referring on this question , I have a similar -but not the same- problem..
I need a way to bind POJO objects to an external entity, that could
I am having a couple of issues deciding how best to describe a certain
I have a problem and I am not sure how to approach the solution.

Trending Tags

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

Top Members

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.