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

In reply to Re: how to get numbers only from n numbers or more digit numbers? by AnomalousMonk
in thread how to get numbers only from n numbers or more digit numbers? by virudinesh

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.