$data =~ s/ < # First '<' (?! # Not followed by (Everything in this list is allowed) (?: # (with non-grouping parens) \/?br> # A break tag | # or \/?p> # A paragraph tag | # or \/?font[^>]*> # A font tag | # or \/?h[1-6]> # A headline ) # Close non-grouping parens ) # End of negative lookahead ( # Capture to $1 [^>]* # Everything until the final '>' ) # End capture > # Final '>' /<$1>/gsix;