in reply to Another regexp question
To be more elaborate, I have constructed the following example to demonstrate how to capture into a hash and an array.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;
And the output is -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);
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.$VAR1 = { 'dogs' => '$20', 'rabbits' => '$10' }; $VAR1 = [ '555-1212', '0404-120021' ];
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Another regexp question
by wolis (Scribe) on Nov 20, 2003 at 04:06 UTC | |
by Roger (Parson) on Nov 20, 2003 at 04:32 UTC | |
by wolis (Scribe) on Nov 21, 2003 at 03:45 UTC | |
by Roger (Parson) on Nov 21, 2003 at 03:53 UTC | |
by davido (Cardinal) on Nov 21, 2003 at 04:01 UTC | |
by wolis (Scribe) on Nov 24, 2003 at 02:54 UTC |