Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Take digits from a string

by Anonymous Monk
on Nov 26, 2012 at 12:59 UTC ( #1005640=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

 my $string= "nPqwe=2456fnraqe=13910feoption";

Hello Monks, I have a simple doubt, can anyone pls answer me?

I have a string as mentioned above. I need to extract only the digits from it ie, i need the output as "245613910". Is it possible to get this using any regex,pls?

Comment on Take digits from a string
Download Code
Re: Take digits from a string
by ww (Bishop) on Nov 26, 2012 at 13:03 UTC
    Tutorials
    perldoc perlretut
    and company.

    Self-help is highly encouraged here!

      Sure,

      Regex is the way to do these kind of things. Can be confusing for starters though. Try to look for regexbuddy (Win software) and a tutorial on Youtube. It's very powerful software.

        No downvote, but this is rather questionable advice for two reasons:

        1. The s/w you recommend is a USD40.00 item
        2. Very loosely, PM is about helping folk learn how to help themselves (or to help them learn, period). IMO suggestions leading the OP to read the docs and search the site provide that in a global fashion far better than offering solutions requiring commercial s/w.
Re: Take digits from a string
by Anonymous Monk on Nov 26, 2012 at 13:32 UTC

    Hello Monks, I have a simple doubt, can anyone pls answer me?

    Yes, you can answer this yourself in about 1 minute by reading perlintro or perlrequick, and probably perlfaq or perlmonks

     [digit string] digit string -> Number of digits at the end of the string

    #!/usr/bin/perl -- use strict; use warnings; use Data::Dump; use Carp::Always; use Regexp::English; my $string = "nPqwe=2456fnraqe=13910feoption"; my $rex = Regexp::English -> remember -> digits -> end ;;;; print "$rex\n"; while( $string =~ m{$rex}g ){ print "$1\n"; } __END__ (?^:(\d+)) 2456 13910
Re: Take digits from a string
by 2teez (Priest) on Nov 26, 2012 at 13:56 UTC

    "..Is it possible to get this using any regex,pls?.."
    Yes, something like so:

    my $string = "nPqwe=2456fnraqe=13910feoption"; if ( my @digits = $string =~ m/[0-9]/g ) { print @digits; # print 245613910 }
    Please also check perlrequick

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: Take digits from a string
by johngg (Abbot) on Nov 26, 2012 at 17:35 UTC

    See tr in Quote and Quote like Operators (it is quite a long way down the page so be patient :-).

    $ perl -E ' > $str = q{nPqwe=2456fnraqe=13910feoption}; > ( $newStr = $str ) =~ tr{0-9}{}cd; > say $newStr;' 245613910 $

    I hope this is helpful.

    Cheers,

    JohnGG

Re: Take digits from a string
by rcrews (Novice) on Nov 26, 2012 at 22:33 UTC
    Here's the simple solution you asked for:
    $string =~ s/[^\d]//g;
    But as several people have already mentioned, you really should read the regex docs -- perlretut, perlre, etc. Knowing all about regular expressions will greatly help you process text with Perl.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2014-12-28 01:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (177 votes), past polls