Re: match nested tags



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?

Here is a general solution for capturing nested constructs from
Jeffery's book "Mastering Regular Expressions" 2nd edition. it counts
the number of opening/closing constructors and return contents between
two balanced constructors.
____________________________________________
use strict;
use warnings;

my $str=<<'END_TEST';
{{ {A} this is part A of the document
{{ {A.1} this is part A1 }}
}} ABCDEFGUI {{ {A} this is part A2 of the document
{{ {A.2} this is part A2 }}
}}
END_TEST

my @x = findtag($str, "A");
print join "\n=new=\n", @x;

#######################
sub findtag {
my ($string, $tag) = @_;
use re 'eval';
my $opening = qr/\{\{/;
my $closing = qr/}}/;
my $normal = qr/(?!$opening|$closing)./s;
my $nested = qr/
(?{ local $n = 0 })
(?>
(?:
$normal+
| $opening (?{ $n++ })
| $closing (?(?{ $n != 0 }) (?{ $n-- }) | (?!) )
)*
)
(?(?{ $n != 0 })(?!))
/x;
my @contents;
push @contents, $1
while $string =~ /$opening\s*\{$tag}\s*($nested)\s*$closing/go;
return wantarray ? @contents : $contents[0];
}
__________________________

Xicheng

.



Relevant Pages

  • Re: match nested tags
    ... DJ Stunks wrote: ... > is there a simple way using regular expression to find nested tags? ... > for example, the string is: ...
    (comp.lang.perl.modules)
  • 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)
  • 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)