use 5.010_00 my $reject_rx = qr{ (?| # either match will be in $1 total [ ] rows [ ] rejected: (\d+) | (\d+) [ ] rows [ ] rejected ) }x; if ( my ($count) = $line =~ /$reject_rx/ ) { say $count; }