in reply to while =~ consecutive matching problem

Show us what is contained in $pattern. If it is just "xy", what you have posted wont find it anyway if xy is embedded between non-space characters. Your existing regex doesnt seem to reflect the behavior you're describing.


Dave

  • Comment on Re: while =~ consecutive matching problem

Replies are listed 'Best First'.
Re^2: while =~ consecutive matching problem
by p.s (Initiate) on Nov 12, 2004 at 16:35 UTC
    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";

      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