use strict; use warnings; use 5.010; my $str =<<'ENDOFSTRING'; unit 53 { description 1234-rwan-1; vci x.yy; } unit 54 { description 45ff-rwan-1; vci x.yz; ENDOFSTRING open(my $INPUT, '<', \$str) or die "Couldn't open string for IO: $!"; while (my $line = <$INPUT>) { if ($line =~ /^ \s+ description \s (\w{4}) (- \w+ - \w+); /xms ){ say "$1 <=> $2"; } } --output:-- 1234 <=> -rwan-1 45ff <=> -rwan-1