Re: match nested tags



DJ Stunks wrote:
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 }}
> }}

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__
{{ {A} this is part A of the document
{{ {A.1} this is part A1 }}
}}

-jp

.



Relevant Pages

  • Re: match nested tags
    ... FangQ wrote: ... is there a simple way using regular expression to find nested tags? ... for example, the string is: ...
    (perl.beginners)
  • Re: match nested tags
    ... is there a simple way using regular expression to find nested tags? ... for example, the string is: ... the number of opening/closing constructors and return contents between ...
    (perl.beginners)
  • match nested tags
    ... is there a simple way using regular expression to find nested tags? ... for example, the string is: ... can anyone give some hint? ...
    (perl.beginners)
  • Re: Get regular expression
    ... own tree structure. ... Expression compares a string character-by character, ... regular expression solution, which was about as close as one could get to ... the structure of the hierarchy can be inferred by using ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Get regular expression
    ... regular expression solution, which was about as close as one could get to ... first string. ... explode "ABLATION" and see subnodes of "ENDOMETRIAL ... "Heart 27.33/2" ...
    (microsoft.public.dotnet.languages.csharp)