Whew.  I'm glad you both explained this to me.  I thought I had to go
change my code for a minute -- but it did indeed work, since the assumption
of always-alphanumeric (plus colons or vertical bars as delimiters) is
correct.

Thanks,

Scott

Scott E. Robinson
SWAT Team
UTC Onsite User Support
RR-690 -- 281-654-5169
EMB-2813N -- 713-656-3629


                                                                                       
                                     
                      "Rob Dixon"                                                      
                                     
                      <[EMAIL PROTECTED]       To:       [EMAIL PROTECTED]             
                                    
                      .co.uk>                  cc:                                     
                                     
                                               Subject:  Re: Preventing null string 
from matching                           
                                                                                       
                                     
                      03/25/03 02:03 PM                                                
                                     
                                                                                       
                                     
                                                                                       
                                     



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]






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

Reply via email to