Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Exact Word Search While Using \Q & \E

by Gwalchmai (Novice)
on Apr 10, 2008 at 14:29 UTC ( #679490=perlquestion: print w/replies, xml ) Need Help??

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

I am using the following code to search for the contents of $term, but need to find the exact word in $term.

For example if $term = 'care', if need to find everything that has "care" and not "careful", "scare", etc.

if ($form{'field'} eq "everything") { $findit .= "/\Q$term\E/i $param "; }




$reg = eval "sub { $findit; }";

Can anyone help me out?

Replies are listed 'Best First'.
Re: Exact Word Search While Using \Q & \E
by prasadbabu (Prior) on Apr 10, 2008 at 14:33 UTC

    Hi Gwalchmai,

    You have to use word boundary or any anchors to match the exact word. For example,


    Also take a look at perlre to know more about word boundary and anchors '^', '\A', '\Z' and '$'.


      Thanks Prasad,

      I tried substituting the \Q and \E with \b and the program quit matching anything.

      Also tried putting the \b in front of the \Q, and behind the \E, but that didn't match anything either.

      I'm really confused.


        Here's another problem:
        ... $findit .= "/\Q$term\E/i $param ";
        It appears that you want the match to be case insensitive when matching $term. The way to specify that is as follows:
        $findit_re = "\\b(?i:\Q$term\E)\\b";
        This turns on case-insensitive matching for $term. Also, note the use of double-slashes since double quotes are being used. An easier way to write this is to use qr//:
        $findit_re = qr/\b(?i:\Q$term\E)\b/;

        At first I thought you were confused about the use of \Q...\E. After more carefully reading your response I don't think this is the case, but here is what I wrote about it:

        \Q and \E is simply another way of invoking the the quotemeta() function - it just escapes characters which are used to denote regular expression elements.

        For instance, suppose you wanted to search for the three character sequence [a], i.e. a left bracket, the letter 'a', and then a right bracket, If you used the regular expression:

        my $target = "[a]"; if ($string =~ m/$target/) { ... }
        it wouldn't work as you wanted because the brackets would be be interpreted as being part of the character class regex element. You can fix things by using quotemeta or \Q...\E as follows:
        my $target = "[a]"; if ($string =~ m/\Q$target\E/) { ... } # or: my $re = quotemeta($target); if ($string =~ m/$re/) { ... }
        Now you'll only get a match if $string contains the three character sequence [a].

        Another way to think about it is that the quotemeta is a function which converts a string to a regular expression which matches exactly that literal string.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2022-05-25 03:54 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (84 votes). Check out past polls.