$str = 'examplestringofrandomlength/userid=6&refid=49';; ( $num1, $num2 ) = $str =~ m[userid=(\d+)[;&]refid=(\d+)];; print for $num1, $num2;; 6 49