foo a b cd efg bar #### my @matches; if ($data =~ m/foo (.*) bar/) { # everything between foo and bar goes into $1 @matches = split(' ', $1); # split on whitespace }