Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

regexp bug

by kidd (Curate)
on Aug 27, 2002 at 13:36 UTC ( [id://193153]=perlquestion: print w/replies, xml ) Need Help??

kidd has asked for the wisdom of the Perl Monks concerning the following question:

hello monks...

I have this code that makes a search trough a lot of texts. I finished it and I tested but the I saw something, when I searched for lets say "foo" it returned true to either "foo" and "foobar"... I didnt want that! I wanted only to be true to "foo" nothing more nothing less.

So, I decided to make a correction and I edited this line:

my $count = grep{ /$query/ } $_;
to
my $count = grep{ /\b$query\b/ } $_;

But I test it and sometimes it works fine and other times it doesnt...Can someone tell me what Im doing wrong...

And if you could tell me how can I make the search not case-sensitive...

I thought that maybe I could do that like this:

my $count = grep{ /\b$query\b/i } $_;

THANKS

Replies are listed 'Best First'.
Re: regexp bug
by hotshot (Prior) on Aug 27, 2002 at 15:24 UTC
    But I test it and sometimes it works fine and other times it doesnt

    can you give us some examples? (on what strings it works and when it doesn't work).

    Hotshot
Re: regexp bug
by thelenm (Vicar) on Aug 27, 2002 at 17:10 UTC
    As hotshot said, giving examples of how it's failing will be very helpful. Please give some examples of input you're giving it and output you expect.

    It's not clear what $count is supposed to be, but it will actually be either 0 or 1 depending on whether the pattern matches $_. If you want a true count of the number of matches, you should do something like this instead:

    $count = () = /\b\Q$query\E\b/ig;
    I put the \Q and \E in there even though "foo" doesn't contain any special regex characters, because it guards against accidents if you search on something besides "foo" later. And you're correct to use /i to do a case-insensitive match.

    -- Mike

    --
    just,my${.02}

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://193153]
Approved by jlongino
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-19 11:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found