On Sat, Jul 05, 2003 at 11:29:12AM -0400, Jeff 'japhy' Pinyan wrote:
> On Jul 3, George P. said:
> 
> >> while ($string =~ /pattern/g){
> >>   $count++;
> >>   if ($count > $max_count){
> >>     $string = substr($string,0,pos($string));
> >>     last;
> >>   }
> >> }
> >
> >$string =~ s/((.*?$pattern){$max_count})(.*)/$1/s;
> 
> You don't need to capture the .* at the end of the regex.  This is one of
> those cases where I my \K anchor/assertion idea would really come in
> handy:
> 
>   s/(?:.*?$pattern){$max_count}\K.*//s;
> 
> What the \K does is make the regex think it JUST started matching, so
> instead of replacing a bunch of stuff plus some extra fluff with the
> original bunch of stuff, we just say "after you've matched X, pretend you
> started matching HERE."  It comes in handy in substitutions that look like
> 
>   s/(A)B/$1/;

Honest question: What's the advantage of doing it with the \K anchor
as opposed to simply not matching the C<B> section at all?  (Assuming,
of course, that you don't B<need> to match it, e.g. to disambiguate
your match.)


--Dks

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to