#!/usr/bin/env perl use Modern::Perl; local $/ = ""; # split records on a blank line while () { if ( m[Orderbook\ ID: \s* (.+?) \n \s* Symbol: \s* (.+?) \n \s* ISIN: \s* (.+?) \n ]xs ){ say "$1:$3:$2"; # comment the previous line and uncomment the next three if you really # want to strip whitespace from your values, unlike your first # example # my $l = "$1:$3:$2"; # $l =~ s/\s+//g; # say $l; } } __DATA__ [Sample data removed by request of the original poster.]