Re: Find number of matches using Match

Ronald J Kimball <rkimball-jkI2qz8QNLsmlAP/[email protected]> Mon, 21 May 2012 22:48:30 -0400
Newsgroups gmane.comp.lang.perl.modules.template-toolkit
Message-ID <CAEnkCGQ3e3ZBtC_9Sk2zJC8iK22zcsJGOramEGHMn69AK9cruA@mail.gmail.com>
On Mon, May 21, 2012 at 9:44 PM, Summer <crazysummer2004-/[email protected]> wrote:

> Hey,
>
> I am using the "match" method and I am looking for a regex in a string.
> For the sake of argument, lets say we are looking for "no" in the following.
>
> testString =""no, i do not believe we could not go forward to the sea. I
> know. no;
>
> so,
> valHolder = testString.match("(?i:no)");
>
> valHolder.size -> always gives me 1. Yet I am expecting 4 matches.
>
> Is there a way I can know the count of the matches?
>
>
http://template-toolkit.org/docs/manual/VMethods.html#method_match

       To perform a global search to match the pattern as many times as it
       appears in the source string, provide a true value for the "global"
       argument following the pattern.

           [% text = 'bandanna';
              text.match('an+', 1).join(', )      # an, ann
           %]

So, to get the count:

[% text.match('no', 1).size %]


Ronald

_______________________________________________
templates mailing list
[email protected]
http://mail.template-toolkit.org/mailman/listinfo/templates