#### my $txt = "Some long html document"; my $tag = "img" my $matches = ( $txt =~ s% (<\Q$tag\E\s+ # begin with tag and space (?: \w+ # key/bareword (?: =(["']?) # begin with quote or not (?:|.*?[^\\]) # some value that doesn't end with \ \2 # close quote maybe )? # possible bareword (?:\s+|>) # something trailing (force match) )+ # multiple groups >?) # trailing > (handles <$tag word=val >) %&tag_handler($1)%gexis );