my (@match) = $str =~ / (?: ^foo\ (?= (?:m\ \d+\ )+bar) # overall match from ^foo | (?