Click here to get back home

Re: match nested tags

 HomeNewsGroups | Search | About
 comp.lang.perl.modules    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
Re: match nested tags DJ Stunks 05-03-2006
Posted by DJ Stunks on May 3, 2006, 5:20 pm
Please log in for more thread options


Hey guys, I'm not getting any responses over at perl.beginners so I
thought I'd cross post this here to see if anyone has any ideas.

Here's the original message:

FangQ wrote:
> hi
>
> is there a simple way using regular expression to find nested tags?
>
> for example, the string is:
>
> {{ this is part A of the document
> {{ this is part A1 }}
> }}
>
> I want to define a function findtag("A") to give me
>
> this is part A of the document
> {{ this is part A1 }}
>
>
> and findtag("A.1") to give me
>
> this is part A1
>
> can anyone give some hint?
> thanks

I thought this sounded like a prime candidate for Parse::RecDescent,
but I can't get the nested nature of the part(s) to work.

Here's my first crack at it, but it doesn't parse. I monkeyed with it
for a while, but to no avail.

I did note, however, that in the Parse::RecDescent FAQ, Pastor Conway
suggests using Text::Balanced to extract nested parenthesis. I tried
that too, but again, no luck.

I'd be interested to see if anyone here has a suggestion for this
problem. Thanks in advance.

#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;
use Parse::RecDescent;

my $grammar = <<'EO_GRAMMAR';
        <autotree>

        document : '{{' part(s) '}}'

        part : part_id part_text part(s?)
        part_id : '{' /[^}]+/ '}'
        part_text : /.+/s

EO_GRAMMAR

my $parser = Parse::RecDescent->new($grammar)
        or die "Could not parse grammar: $@";

my $document = do {local $/; <DATA>};

my $doc_ref = $parser->document($document)
        or die "Invalid document";

print Dumper $doc_ref;


__DATA__
{{ this is part A of the document
        {{ this is part A1 }}
}}

__END__

-jp


Posted by DJ Stunks on May 3, 2006, 6:04 pm
Please log in for more thread options


DJ Stunks wrote:
> FangQ wrote:
> > is there a simple way using regular expression to find nested tags?
> >
> > for example, the string is:
> >
> > {{ this is part A of the document
> > {{ this is part A1 }}
> > }}

Allow me to just reply to myself here... :P

I repaired my crummy grammar and posting technique (who would have
thought __END__ would end up in __DATA__?). My grammar now parses and
is shown below (getting there!), now I need to concentrate on getting
the output hash right.

Also, I'm not able to have a { or } in the part_text, which I expect
would be a problem in the real world.... I don't know how to
incorporate Text::Balanced here though....

I'll keep working on it.

#!/usr/bin/perl

use strict;
use warnings;

use Data::Dumper;
use Parse::RecDescent;

my $grammar = <<'EO_GRAMMAR';
<autotree>

document : part(s)

part : '{{' part_id part_text part(s?) '}}'
part_id : '{' /[^}]+/ '}'
part_text : /[^{}]+/

EO_GRAMMAR

my $parser = Parse::RecDescent->new($grammar)
or die "Could not parse grammar: $@";

my $document = do {local $/; <DATA>};

my $doc_ref = $parser->document($document)
or die "Invalid document";

print Dumper $doc_ref;

__DATA__
{{ this is part A of the document
{{ this is part A1 }}
}}

-jp


Similar ThreadsPosted
how to check for existenceof XML tags with XML::Simple? January 29, 2005, 12:45 pm
XML::LibXSLT element tags stripped out of transformation March 21, 2005, 6:32 pm
Help with Net::IP::Match and mod_perl September 8, 2004, 8:13 pm
Net::Telnet $prematch, $match April 12, 2007, 10:18 am
Net::Netmask -> match usage problems April 14, 2005, 10:03 am
Nested loops? November 12, 2005, 10:07 pm
DBD::mysql object version 2.9003 does not match bootstrap parameter March 4, 2005, 9:00 am
Creating Makefiles for multiple nested modules June 8, 2006, 3:33 pm
HTML::Template arbitraryily nested recursive loops April 8, 2005, 2:59 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap