use strict; my $string = "This is a regex test"; my ($condition, $match); if ($condition) { $string =~ /(is a)/; } else { $string =~ /(regex)/; } $match = $1; print $match."\n";