I have a string like this:

$_ = "This is list number 12. It contains apples, pears, peaches. Total cost is 5.";

and I want to extract the elements of the list (apples, pears, peaches). I would like to do this using a single regex.
What I came up with is this:

my (@rep) = /It contains (?:\s*([^,]*),)*\s*([^\.]*)\./;

But even though this does match the text, @rep contains just pears and peaches.
Am I missing something trivial or should I just give up on the single regex idea and use multiples regexes and/or split to get what I want?

The example above is made up, but the actual strings look structurally the same: a preamble, followed by a variable number of <text><separator> pairs, followed by a postamble

Update: ikegami++ for the most practical solution, davido++ for a great single-regex solution

Update: pijll++ for doing both practical and single-regex


In reply to Regex to extract multiple occurrences by Crackers2

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.