Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

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?

Replies are listed 'Best First'.
Re: Take digits from a string
by ww (Archbishop) on Nov 26, 2012 at 13:03 UTC
    perldoc perlretut
    and company.

    Self-help is highly encouraged here!


      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 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.



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 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.
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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1005640]
Approved by ww
holli gotta grab some groceries before dawn. l8ters
[erix]: s/dawn/dusk/
[erix]: (you're welcome :P)
[james28909]: you know, i had a thought a few mnths back. what if this universe were just a puzzle, waiting for life to figure it out!
[james28909]: what if one day we are able to grab the curtain that is "spacetime" and pull it back to reveal <insert imagination here> pulling at levers and ropes xD
[holli]: like dorothy?
[james28909]: see ya later holli
[james28909]: yeah like wizard of oz lol. its just a joke though
[james28909]: i cannot understand how someone cant see that a cloud of gas that evolves into a solar system or galaxies. it just makes sense to me. everythign that has happened, had to have happened for anything on this planet to be.
[james28909]: /gas/gas and debris/

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (14)
As of 2017-12-15 14:43 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (433 votes). Check out past polls.