This one finds nested quotation marks (<>[]{}()) and prints the text inside them. This is my first posting here and i'm always looking for a better way.
# Parses nested quoted text and returns each quoted text. # Copyleft: 2001 Sakis Doupas, adoupas@xanthi.ilsp.gr $string = $ARGV[0] or die "gimme a string\n"; $quotes = "(,)" unless $quotes = $ARGV[1]; ($q1, $q2) = split(/,/, $quotes); @quotes = (); sub parse1 { my $str = shift(@_); if ($str =~ /\Q$q1\E/) { push @quotes, $'; &parse1($'); } &parse2; } sub parse2 { my $str = pop(@quotes); if ($str =~ m/\Q$q2\E/) { my $temp = $`; foreach $value (@quotes) { $value =~ s/\Q$q1$temp$q2\E/_lq_$temp\_rq_/; } $temp =~ s/_lq_/$q1/g; $temp =~ s/_rq_/$q2/g; print $q1 . $temp . $q2 . "\n"; } } &parse1($string);

Replies are listed 'Best First'.
Re: parse nested text
by arhuman (Vicar) on Jul 09, 2001 at 19:18 UTC
Re: parse nested text
by grinder (Bishop) on Jul 10, 2001 at 13:09 UTC

    Don't mean to pour water on your fire, but doesn't this do what Text::DelimMatch already does? I'd tend to go with the CPAN solution (and in fact I do use it in production code from time to time) as it's more complete and no doubt deals with a gotcha or two that you haven't yet encountered.


    --
    g r i n d e r
Re: parse nested text
by I0 (Priest) on Jul 10, 2001 at 03:09 UTC
    ($re=$_)=~s/((\Q$q1\E)|(\Q$q2\E)|.)/${['(','']}[!$2]\Q$1\E${[')','']}[ +!$3]/gs; @$ = (eval{/$re/},$@); print join"\n",@$ unless $$[-1]=~/unmatched/;