Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

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 (Bishop) 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
Corion is mildly surprised as some different toy projects coagulate into a useful application. I've been toying with writing a Meetup wrapper and also have been toying with synchronizing events between (CalDAV) calendars, and now that means I can ...
[Corion]: ... easily* synchronize my personal calendar with Meetup and import stuff from Meetup there
[Corion]: * Code not yet written, some restrictions may apply

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (11)
As of 2017-02-23 09:35 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (343 votes). Check out past polls.