Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Stripping out specific numbers

by mrras25 (Acolyte)
on Dec 12, 2013 at 00:03 UTC ( #1066749=perlquestion: print w/replies, xml ) Need Help??
mrras25 has asked for the wisdom of the Perl Monks concerning the following question:

Good Afternoon/Evening,

I need to know what is the best way to strip out numbers that are 4 to 5 digits in length in a string.


string: npnoa72932n02

Need : 72932 (numeric string of 5)

String: geed7812r412p

Need : 7812 (numeric string of 4)

Thank you for your help

Replies are listed 'Best First'.
Re: Stripping out specific numbers
by davido (Archbishop) on Dec 12, 2013 at 00:09 UTC

    What have you tried?


    This will capture.

    if( $string =~ m/(?<!\d)(\d{4,5})(?!\d)/ ) { print "$1\n"; }

    If you want to capture all occurrences:

    my( @found ) = $string =~ m/(?<!\d)(\d{4,5})(?!\d)/g;

    And if "strip" means remove:

    $string =~ s/(?<!\d)\d{4,5}(?!\d)//g;


Re: Stripping out specific numbers
by AnomalousMonk (Chancellor) on Dec 12, 2013 at 00:12 UTC

    One way:

    >perl -wMstrict -le "my $s = 'xx123xx4321xx56789xx987654xx'; ;; my @n = $s =~ m{ (?<! \d) \d{4,5} (?! \d) }xmsg; printf qq{'$_' } for @n; " '4321' '56789'
Re: Stripping out specific numbers
by mrras25 (Acolyte) on Dec 12, 2013 at 14:48 UTC
    Thank you all for the help - I used a combination of the 2 replies to get the disired result. To answer what I done prior to that, it was very basic. First I matched 4 or 5 number strings and then I striped out any characters leaving undisered results.
    if($string =~ m/(\d\d\d\d+)/ || $string =~ m/(\d\d\d\d\d+)/){ $string =~ s/\D//g; }
    As you can see not so good and not so desired... again thank you for your help.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1066749]
Approved by Old_Gray_Bear
[1nickt]: choroba Understood. I'm wondering about the logic in a cpanfile. If the perl doesn't support threads, it's easy: require MCE. If the perl does support threads, as you say the user has a choice, so require both? Or, assume that irrespective of the choide
[1nickt]: ... choice, the threaded-perl user *probably* wants to use threads, therefore only require threads and leave MCE out, allowing the user to manually install if desired?
[choroba]: just require any of MCE and threads. Not sure if that's possible in the cpanfile, but should be possible early in the Makefile
[1nickt]: The app expects threads as default anyway, right?
[choroba]: that's true
[1nickt]: expects threads *to be* the default
[1nickt]: I think cpanfile can handle the logic. testing now.

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (14)
As of 2017-10-18 13:24 GMT
Find Nodes?
    Voting Booth?
    My fridge is mostly full of:

    Results (244 votes). Check out past polls.