use Regexp::NamedCaptures; if ( /^(?< \$number >\d+) (?:MatchMe \[(?< \$reason_why >.*)\]|I need to be dispatched.*:\s(< \$reason_why >.*))$/ ) { # $reason_why got set from either $2 or $3 but we don't really care about the numbered variables now. print "$number $reason_why\n"; }