my $test_str = "1 text text text 2 text 3 "; my $test_out = q{}; my $sec_num_comb_pat = $empty_str; $sec_num_comb_pat .= '^(([0-9]+)(?![^0-9]*[0-9]))'; my $sec_num_rx = qr{$sec_num_comb_pat}xms; if ( $test_str =~ m{($sec_num_rx)}xms ) { $test_out = $1; } print "\$test_out\n" . $test_out . "\n"; #### foreach (@test) { @test_filtered=grep{/^(([0-9]+)(?![^0-9]*[0-9]))/xsm} @test; }