Another single regular expression, with the @array = $str =~ /regexp/ idiom -
my $str = 'The rabbits is $10 and the dogs are $20. The phone number i +s 555-1212.'; # updated: thanks to davido to point out the interpolation # of $10 and $20 in my double quoted string. I have changed # the double quote to single quote. my @capture = $str =~ /(rabbits|dogs|\d+-\d+)/g; print "$_\n" for @capture;
To be more elaborate, I have constructed the following example to demonstrate how to capture into a hash and an array.

use strict; use Data::Dumper; my $str = 'The rabbits is $10 and the dogs are $20. ' . 'The phone number is 555-1212, mobile number 0404-120021'; my $animal = "rabbit|dog"; my %prices = $str =~ m/((?:$animal)s?)\s(?:is|are)\s(\$\d+)/g; my @phone = $str =~ m/(\d+-\d+)/g; print Dumper(\%prices); print Dumper(\@phone);
And the output is -
$VAR1 = { 'dogs' => '$20', 'rabbits' => '$10' }; $VAR1 = [ '555-1212', '0404-120021' ];
In general the complexity of the regular expression increases if the number of requirement increases, as well as the complexity of your sentense structure. You will have to pick one best suited to your data.

And of course if you want to parse natural language, automatically recognise what is an animal, and pick out the price from a complex sentense, it will be a mammoth task indeed.

Try pick out the prices from the following sentense :-)

I have a dog and two cats, I will charge you $10 for each cat, but I won't sell the dog to you for $10, I will have to charge you $20 extra.


In reply to Re: Another regexp question by Roger
in thread Another regexp question by carric

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.