in reply to Re: Re: Another regexp question
in thread Another regexp question
If you want to capture all occurances of the patterns, you could use the @array = $str =~ m/pattern/g idiom.my $something = 'This is a bang of a bing thing'; my @list = qw /bing bong bang/; # want to search for these my $list = join '|', @list; # construct my pattern if($something =~ m/($list)/i) { print "Found '$1' in '$something'\n"; }
or you could do this in a while loop -my @search = $something =~ m/($list)/ig; # <- added the g modifier
The problem with your code is that m/(@list)/i is looking for the pattern of the interpolated list items, the pattern "bing bong bang", in the string, and of cause it is not found.while ($something =~ m/($list)/ig) { print "Found '$1' in '$something'\n"; }
And the output is -use strict; my $something = 'This is a bang of a bing thing bing bong bang'; my @list = qw / bing bong bang /; if ($something =~ m/(@list)/i) { print "Found '$1' in '$something'\n"; }
Found 'bing bong bang' in 'This is a bang of a bing thing bing bong ba +ng'
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Re: Re: Another regexp question
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 |