$s = "abc\ncd\ne\n\npqr\nst\nf\n\n"; while( $s =~ m[ (\w+) \n (\w+) \n (\w+) ]gx ) { print "$1:$2:$3"; } abc:cd:e pqr:st:f