Ask a Question related to PERL Modules, Design and Development.
-
DJ Stunks #1
Re: match nested tags
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:I thought this sounded like a prime candidate for Parse::RecDescent,> hi
>
> is there a simple way using regular expression to find nested tags?
>
> for example, the string is:
>
> {{ {A} this is part A of the document
> {{ {A.1} this is part A1 }}
> }}
>
> I want to define a function findtag("A") to give me
>
> this is part A of the document
> {{ {A.1} this is part A1 }}
>
>
> and findtag("A.1") to give me
>
> this is part A1
>
> can anyone give some hint?
> thanks
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__
{{ {A} this is part A of the document
{{ {A.1} this is part A1 }}
}}
__END__
-jp
DJ Stunks Guest
-
How to treat template tags as comment tags?
Hi all, I am using DW MX to edit templates for a FreeMarker application. The template tag fromat is like so: <ul> <#list birds as bird>... -
Parsing nested Tags
Hi, i'm a bit stuck at the following problem and hope any of you can provide me wit a nice solution: I have a company with multiple employees.... -
Why is this not a match?
Hello All, I am unable to find out why is this not matching: I do have a file where the second line contains: Summary Log (generated: Tue... -
Problem overriding render method to format literal content of nested tags in custom control
Peter, I was under the mistaken impression that I was already overriding CreateChildControls to create the controls with the code: protected... -
Using ParseChildren attribute to load child tags - VS removes tags
I am building a poll control, nested in the tag I have child tags to setup the poll options. Everything works fine, but when I edit a property in... -
DJ Stunks #2
Re: match nested tags
DJ Stunks wrote:
Allow me to just reply to myself here... :P> FangQ wrote:> > is there a simple way using regular expression to find nested tags?
> >
> > for example, the string is:
> >
> > {{ {A} this is part A of the document
> > {{ {A.1} this is part A1 }}
> > }}
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__
{{ {A} this is part A of the document
{{ {A.1} this is part A1 }}
}}
-jp
DJ Stunks Guest



Reply With Quote

