< Parsing XML with Regular Expressions (cont.) >


Ex 1. A script to search for the question tag
open(FILE,'msfaq.xml');
my $file = <FILE>;
$tag = 'question';
while ($file =~ /<$tag(.*?)(\/>|>(.*?)<\/$tag>)/gsi) {
    my @attribs = split(/\s+/,$1);
    my ($attr,$key,$value,%attr_hash);
    foreach $attr (@attribs) {
        next if !$attr;
        ($key,$value) = split(/=/,$attr);
        $value =~ s/"//g;
        $attr_hash{$key} = $value;
    }
    foreach $key (keys(%attr_hash)) {
        print "$key = $attr_hash{$key}\n";
    }
    print "$tag = $1 - $3\n";
}