$data =~ s/ ( # Capture to $1 ](?!href) # All non > not followed by href )* # zero or more of them .? href\s* # href followed by zero or more space characters ) ( # Capture to $2 &\#61;\s* # = plus zero or more spaces ( # Capture to $3 &[^;]+; # some HTML character code (probably " or ') )? # which might not exist (?: # Non-grouping parens .(?!\3) # any character not followed by $3 )+ # one or more of them .? (?: \3 # $3 )? # (which may not exist) ) ( # Capture to $4 [^>]+ # Everything up to final > > # Final > ) /$1 . decode_entities($2) . $4/gsexi;