in reply to Re^2: Need a Regular Expression that tests for words in different order and captures the values found.
in thread Need a Regular Expression that tests for words in different order and captures the values found.
Hmmmm....good point. Well, you can fix part of it with (?=.*\bfred\b (\w+)), but avoiding matching none of the targets is still a problem. The way I suggested will always get three matches because it says find "zero or more of this". That means either you can't actually find out how many non-empty matches you got, or you can only match when all the targets are present. Separate matches in a loop, as suggested by several others, is the way to go. Here's my take, redux:
$string = "This is bLarney rubble and his friends joe rockhead and fre +d flintstone"; $count = 0; for $target (qw(fred barney joe)) { if ( $string =~ /(?=.*\b$target (\w+))/i ) { push @elements, $1; $count++; } else { push @elements, ''; # as a placeholder } } if ($count >= 2) { print join('_', @elements), "_inc\n" } else { print "Didn't find at least 2 elements in the strin +g\n" } # prints flintstone__blockhead_inc # change 'joe' to 'moe' and you get > Didn't find at least 2 elements +in the string
There ought to be something useful in there. :-)
--marmot
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^4: Need a Regular Expression that tests for words in different order and captures the values found.
by AnomalousMonk (Archbishop) on Jan 15, 2010 at 20:11 UTC | |
by furry_marmot (Pilgrim) on Jan 15, 2010 at 20:47 UTC |