[Israel.pm] recursive regex
Yuval Yaari
yuval at windax.com
Wed Jun 9 03:19:38 PDT 2004
Why don't you try hacking what I started which is based on my stupid XML
indenting script:
As you can see, it's originally written for XML parsing, but you should
easily convert it to fit your needs.
# 8< - - - - - - - - - - - - - >8
#!/usr/bin/perl
-w
use strict;
use diagnostics;
my @Data = <DATA>;
chomp(@Data);
my $indent_level = 0;
my @last_tags = ();
foreach my $line (@Data) {
$line =~ s/^\s+//;
next if (length($line) == 0);
if ($line =~ m!BEGIN_(\w+)!) {
print "\t" x $indent_level;
$indent_level++;
push(@last_tags, $1);
} elsif ($line =~ m!END_$last_tags[-1]!) {
$indent_level--;
pop(@last_tags);
print "\t" x $indent_level;
} else {
print "\t" x $indent_level;
}
print $line . "\n";
}
__DATA__
BEGIN_A
...
END_A
BEGIN_B
BEGIN_C
...
END_C
END_B
__END__
# 8< - - - - - - - - - - - - - >8
I could help you hack this code a bit if you want.
--Yuval
More information about the Perl
mailing list