# extracts the attributes from an HTML tag
# and displays them, separately, with one regex
$text = q{
};
@section = ('attributes');
$text =~ m{
(?{ @s = map [], 1 .. @section })
< \w+
(?:
\s+
(?{ local $s[0] = [ @{ $s[0] }, [ length "$`$&" ] ] })
\w+
(?{ $s[0][-1][1] = length "$`$&" })
(?:
\s* = \s*
(?:
"
(?{ local $s[1] = [ @{ $s[1] }, [ length "$`$&" ] ] })
[^"]*
(?{ $s[1][-1][1] = length "$`$&" })
"
|
'
(?{ local $s[1] = [ @{ $s[1] }, [ length "$`$&" ] ] })
[^']*
(?{ $s[1][-1][1] = length "$`$&" })
'
|
(?{ local $s[1] = [ @{ $s[1] }, [ length "$`$&" ] ] })
[^\s>]+
(?{ $s[1][-1][1] = length "$`$&" })
)
|
(?{ local $s[1] = [ @{ $s[1] }, [ -1 ] ] })
)
)*
\s* >
(?{ @watch = @s })
}x;
print "The following attributes were found:\n";
for (@watch) {
my $i = 0;
while (my $find = shift @{ $watch[$i++] }) {
my ($s, $e) = @$find;
print("\n"), next if $s == -1;
print "=" if $i == 2;
print substr($text, $s, $e-$s);
print "\n" if $i == 2;
}
}
__END__
output:
The following attributes were found:
src=foo.jpg
ismap
border=0