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

regexp bug

by kidd (Curate)
on Aug 27, 2002 at 13:36 UTC ( #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/ } $_;
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 } $_;


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).

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


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://193153]
Approved by jlongino
[james28909]: cant seem to make it look in linux module path for
[james28909]: no matter how i prepare the paths. oh well. looks like ill just have to start wsl perl from scratch
[Corion]: james28909: You can get a list of all modules on your Windows Perl via the autobundle command in the cpan shell. But that likely lists many more modules than you actually want. I recommend a clear separation and installing modules on ...
[Corion]: ... both Perls separately. I use cpanfiles or Makefile.PL for that - listing all modules for an application there allows me to install them via cpanm . or cpan . automatically without any further interaction

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2018-05-23 17:21 GMT
Find Nodes?
    Voting Booth?