#!/usr/bin/perl use strict; use warnings; my $x = "0.01 NaN 2.30 4.44"; # the following works as desired my $r1 = qr/([Na0-9\.\-\+]+)\s+ ([Na0-9\.\-\+]+)\s+ ([Na0-9\.\-\+]+)\s+ ([Na0-9\.\-\+]+)/x; my ($d, $e, $f, $g) = ($x =~ m/$r1/x ); print qq($d, $e, $f, $g\n); # the following finds the first number twice my $r2 = qr/(?=(([Na0-9\.\-\+]+)\s*)){4}/x; ($d, $e, $f, $g) = ($x =~ m/$r2/x ); print qq($d, $e, $f, $g\n); # the following finds a null prior to the first item my $r3 = qr/((?=([Na0-9\.\-\+]+)\s*){4})/x; ($d, $e, $f, $g) = ($x =~ m/$r3/x ); print qq($d, $e, $f, $g\n); exit(0);