Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

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.

Examples:

string: npnoa72932n02

Need : 72932 (numeric string of 5)

String: geed7812r412p

Need : 7812 (numeric string of 4)

Thank you for your help

Comment on Stripping out specific numbers
Re: Stripping out specific numbers
by davido (Archbishop) on Dec 12, 2013 at 00:09 UTC

    What have you tried?

    perlrequick

    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;

    Dave

Re: Stripping out specific numbers
by AnomalousMonk (Abbot) 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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1066749]
Approved by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (5)
As of 2014-10-24 06:58 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (130 votes), past polls