my $text = 'hello this is a multiline message'; $text =~ s/([^\S\n]+|\n)/$1 eq "\n" ? print "New line\n" : print "Whitespaces\n"; $1/gemi; #### New line Whitespaces Whitespaces Whitespaces Whitespaces New line Whitespaces