Ask a Question related to PERL Modules, Design and Development.

  1. #1

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

    __END__

    -jp

    DJ Stunks Guest

  2. Similar Questions and Discussions

    1. 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>...
    2. 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....
    3. 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...
    4. 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...
    5. 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...
  3. #2

    Default 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

    DJ Stunks Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139