my $reject_rx = qr{ total [ ] rows [ ] rejected: [ ] (\d+) | (\d+) [ ] rows [ ] rejected }x; if ( $line =~ /$reject_rx/ ) { my $count = defined $1? $1 : $2; print $count, "\n"; } #### 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; }