if ($desc =~ m#((?:\S+\s*){1,30})#) { $first_30 = $1; } else { # Didn't match here }