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"; #### if ($condition) { $string =~ /(is a)/; $match = $1; } else { $string =~ /(regex)/; $match = $1; } print $match."\n";