Hi,
A requirement is that it is preceeded and followed by a space. I have included a script that demo's what I am saying if that helps. Thanks :)
#!/usr/bin/perl
$countsAllExample="a b x y i x y p";
$doesntCountAll="a b x y x y p";
$pattern = "x y";
$count=0;
while($countsAllExample =~ /\s$pattern\s/g){
$count +=1;
}
print "found $pattern $count times in $countsAllExample\n";
#now error occurs as only 1 "x y" reported
###########################################
$count=0;
while($doesntCountAll =~ /\s$pattern\s/g){
$count +=1;
}
print "found $pattern $count times in $doesntCountAll\n";
| [reply] |
Ok, take your second test case, the one that doesn't work as you expect, and let's walk through it.
- Match "_x_y_" (I used _ to indicate a space character). One is found, and the pattern match position pointer is advanced to the 2nd 'x'.
- Look for another "_x_y_", but there isn't one, because you're already at position 'x', not position '_' (space).
What you may want is something like this:
m/\s$pattern(?=\s)/
This won't gobble the second space char.
| [reply] [d/l] [select] |