Well, if < _$20_, you *need* this #### use warnings; use strict; sub reg_fix { my $text = shift(@_); # I guessed what you wanted =equals= to indicate: my %tag= qw( / em * b _ u = strike ); my %open; @open{ qw( / * _ = ) }= (); my @tokens= $text =~ m{ ( <[^<>]+> | [/*_=] | [^"; my $nest; do { $nest= pop @nested; undef $open{$nest}; } while( $nest ne $token ); $token= ""; } } } return join '', @tokens; } print reg_fix($_) for ; __END__ go/to /thx/ _silly *to_* Well, if < _$20_, you *need* this #### goto thx/ silly *to* Well, if < $20, you need this