Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

how to get numbers only from n numbers or more digit numbers?

by virudinesh (Acolyte)
on Jun 13, 2013 at 06:45 UTC ( #1038683=perlquestion: print w/ replies, xml ) Need Help??
virudinesh has asked for the wisdom of the Perl Monks concerning the following question:

@f='chapter 01,02,03,04'; foreach(@f) { if($_=~m/,(\d)+|(\d)+,(\d)+|(\d)+,(\d)+,(\d)+/i) { print "$& \n"; } }

am using matching regular expression was only matching 01,02

but am need chapter 01, after all those numbers only like 02,03,04

i have tried different ways but don't get idea.

how to do print only 02,03,04 like n numbers?

Comment on how to get numbers only from n numbers or more digit numbers?
Download Code
Re: how to get numbers only from n numbers or more digit numbers?
by Anonymous Monk on Jun 13, 2013 at 06:59 UTC
    Why are you using $& ?
      $& its matching key word ...

        $& its matching key word ...

        Sure its not

Re: how to get numbers only from n numbers or more digit numbers?
by zork42 (Monk) on Jun 13, 2013 at 07:09 UTC
    Are you saying you want your output to look like this?
    chapter 01 02 03 04
    Also, it might help if you add a few more example elements to @f on line 1.
      chapter 01 no need only 02,03,04

        use warnings; use strict; my $f='chapter 01,02,03,04'; if(my @m=$f=~/,([0-9]{2})/g){ print "@m\n"; #prints 02 03 04 }
        Note: I would rather use a scalar variable, instead of an array variable with only one element like you had.

        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

        split by ",", shift the resulting array, here you are.

        my @results = split /,/,$_; shift @results; print "@results\n";
Re: how to get numbers only from n numbers or more digit numbers?
by AnomalousMonk (Abbot) on Jun 13, 2013 at 16:50 UTC

    I'm confused about what you really need, and this is quite possibly overkill. (Regex  \K operator only available with Perl versions 5.10+.)

    >perl -wMstrict -le "use Test::More 'no_plan'; use Test::NoWarnings; ;; my @vectors = ( [ 'chapters 01,02,03,04', qw(01 02 03 04), ], [ 'chapters 1 , 2 , 3 , 4 , 5', qw(1 2 3 4 5), ], [ 'in chapter 01', qw(01), ], [ 'chapter 101', qw(101), ], [ 'chapters 99, 100 ,101 also', qw(99 100 101), ], [ 'chapters 3, 4, 5 and chapter 9', qw(3 4 5 9), ], [ 'read chapters 3, 4 and chapter 9', qw(3 4 9), ], 'pay particular attention to the next variation', [ 'chapters 3, 4, 5 and 9', qw(3 4 5), ], 'no chapter numbers should be extracted from any of these', [ 'chapter' ], [ 'chapters' ], [ 'chapter ii' ], [ '01,02' ], ); ;; VECTOR: for my $ar_vector (@vectors) { unless (ref $ar_vector) { note $ar_vector; next VECTOR; } my ($s, @expected) = @$ar_vector; my @n = $s =~ m{ (?: \G (?<! \A) \s* , \s* | chapters? \s+) \K \d+ }xmsg; is_deeply \@n, \@expected, qq{'$s' -> (@expected)}; } " ok 1 - 'chapters 01,02,03,04' -> (01 02 03 04) ok 2 - 'chapters 1 , 2 , 3 , 4 , 5' -> (1 2 3 4 5) ok 3 - 'in chapter 01' -> (01) ok 4 - 'chapter 101' -> (101) ok 5 - 'chapters 99, 100 ,101 also' -> (99 100 101) ok 6 - 'chapters 3, 4, 5 and chapter 9' -> (3 4 5 9) ok 7 - 'read chapters 3, 4 and chapter 9' -> (3 4 9) # pay particular attention to the next variation ok 8 - 'chapters 3, 4, 5 and 9' -> (3 4 5) # no chapter numbers should be extracted from any of these ok 9 - 'chapter' -> () ok 10 - 'chapters' -> () ok 11 - 'chapter ii' -> () ok 12 - '01,02' -> () ok 13 - no warnings 1..13

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (21)
As of 2014-10-20 18:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (88 votes), past polls