Rob Dixon wrote:
>
> John W. Krahn wrote:
> >
> > Using a character class like \W won't work properly if $string_to_match
> > is non-null.
> >
> > $ perl -le'
> > $_ = ":B000:L520:M260:M:88:8:M602:";
> > $string_to_match = qr/\w+/;
> > $count = () = /\b$string_to_match\b/g;
> > print $count;
> > '
> > 7
> > $ perl -le'
> > $_ = ":B000:L520:M260:M:88:8:M602:";
> > $string_to_match = qr/\w+/;
> > $count = () = /\W$string_to_match\W/g;
> > print $count;
> > '
> > 4
> >
> > Better to use zero-width positive look-ahead and look-behind.
> >
> > $ perl -le'
> > $_ = ":B000:L520:M260:M:88:8:M602:";
> > $string_to_match = qr/\w+/;
> > $count = () = /(?<=:)$string_to_match(?=:)/g;
> > print $count;
> > '
> > 7
> > $ perl -le'
> > $_ = ":B000:L520:M260:M:88:8:M602:";
> > $string_to_match = "";
> > $count = () = /(?<=:)$string_to_match(?=:)/g;
> > print $count;
> > '
> > 0
>
> This isn't a problem if $string_to_match is non-null, but
> only if it is a regex or, more specifically, contains regex
> metacharacters. If this is the case I would personally
> rather go for:
>
> $count = () = /\W\Q$string_to_match\E\W/g;
>
> but my original code was written on the assumption that
> Scott's substrings were always alphanumeric, as per his
> sample. Since he has since posted that the solution was
> effective I think I was right, but thanks for pointing this
> out.
What I was pointing out is that \W "eats" a character on either side of
$string_to_match while the zero-width assertions do not. So assuming:
$_ = ':XXX:XXX:YYY:YYY:XXX:XXX:';
$string_to_match = 'YYY';
Using \W to anchor the match will only match once:
:XXX:XXX:YYY:YYY:XXX:XXX:
^^^^^
first match
Once the \W matches the ending colon the search continues after the
colon:
:XXX:XXX:YYY:YYY:XXX:XXX:
^
^ search continues at this point
With a zero-width assertion no characters are consumed
:XXX:XXX:YYY:YYY:XXX:XXX:
^^^
first match
:XXX:XXX:YYY:YYY:XXX:XXX:
^
^ search continues at this point
:XXX:XXX:YYY:YYY:XXX:XXX:
^^^
second match
John
--
use Perl;
program
fulfillment
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]