# clean lines with words in $text =~ s/.*? # anything (?: # don't remember me (\w.*\w) # everything from first to last word character # on the line - remember this | # or [^\w\s]{6,} # at least 6 characters that aren't words or # spaces (in a row) - forget me ) .* # the rest of the line /$1/gx; # replace whole line with (\w.*\w) match, # if made.