John W. Krahn wrote:
> Rob Dixon wrote:
> >
> >     my @match = /\W($string_to_match)\W/g;
> >     my $count =- @match;
>
> 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

Hi John.

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.

/R




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

Reply via email to