in reply to Re: while =~ consecutive matching problem
in thread while =~ consecutive matching problem

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";
  • Comment on Re^2: while =~ consecutive matching problem

Replies are listed 'Best First'.
Re^3: while =~ consecutive matching problem
by davido (Cardinal) on Nov 12, 2004 at 16:50 UTC

    Ok, take your second test case, the one that doesn't work as you expect, and let's walk through it.

    1. 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'.
    2. 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.


    Dave