If I understand correctly, your problem is that you're looking for /\sx y\s/ repeatedly, but your search pattern includes the spaces on either side. So the first match consumes the following space, making it unavailable to be matched as the preceeding space for the next match. If you really need the spaces you could do /(?<=\s)$pattern(?=\s)/ but you might do better with /\b$pattern\b/. Additonally, you can get the count just by doing
my $count = () = $currentline =~ /\b$pattern\b/g;
Which evaluates the match in list context, creating a list of all matches. It then assigns that in scalar context (which produces a count of the elements) to $count.

Although, none of this will match your string of abxyxyp, because it doesn't have any spaces at all, so I suspect you mistyped that.


In reply to Re: while =~ consecutive matching problem by Eimi Metamorphoumai
in thread while =~ consecutive matching problem by p.s

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.