my @matches = grep { m/$regex/ } @lines; ($_) = m/$regex/ foreach @matches;