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

Hi monks,

I would like to ask, can I limit the grep regular expression to avoid returning the word fragement?

i.e.

If I grep "run", only the line with word "run" will return to me, not the line with word "running"

  • Comment on grep - serach limited to complete word only

Replies are listed 'Best First'.
Re: grep - serach limited to complete word only
by jwkrahn (Abbot) on Mar 30, 2008 at 14:15 UTC

    You could try it with word boundaries:

    my @stuff = grep /\b run \b/x, @other_stuff;
Re: grep - serach limited to complete word only
by ahmad (Hermit) on Mar 30, 2008 at 14:15 UTC
Re: grep - serach limited to complete word only
by FunkyMonk (Bishop) on Mar 30, 2008 at 14:16 UTC
    Yes, use \b which indicates a "word boundary".
    print grep { m{\brun\b} } qw/run runnable running run awordendingwithr +un/;

    Output:

    runrun

    See perlre.

Re: grep - search limited to complete word only
by ww (Archbishop) on Mar 30, 2008 at 15:46 UTC

    Note that the answers above rely on the array being comprised of single words (which may well be the case), However, if the elements being searched contain multi-word phrases or (for example) sentences (which is not specified in the OP), the results may not be as illustrated.

    #! /usr/bin/perl use strict; use warnings; my @other_stuff = ("run", "running", "runs", "runt", "I run a mile per + day.", "Run, rabbit, run!", "One mile per day is easily runnable"); for my $element(@other_stuff) { print $element ."\n"; } print "-" x20 . "\n"; my @stuff = grep /\brun\b/x, @other_stuff; for (@stuff) { print $_ . "\n"; }

    produces...

    ww@GIG:~/pl_test$ perl greprun.pl run running runs runt I run a mile per day. Run, rabbit, run! One mile per day is easily runnable -------------------- run I run a mile per day. Run, rabbit, run! ww@GIG:~/pl_test$

    Precise specification of a problem-area makes it easier for Perl Monks to provide proper answers (and standard spelling is a "good thing," too).

Re: grep - serach limited to complete word only
by szabgab (Priest) on Mar 30, 2008 at 17:37 UTC
    ack -w

    is your friend.

Re: grep - serach limited to complete word only
by Anonymous Monk on Mar 30, 2008 at 14:27 UTC

    Thank you for all of you who reply this question. :)

    One more question on the word boundary: I read the documentation and the document said,

    "is a spot between two characters that has a \w on one side of it and a \W on the other side of it (in either order), counting the imaginary characters off the beginning and end of the string as matching a \W "

    Can I said the word boundary is any character in class \w?(e,g, space, tab, newline?)

      Can I said the word boundary is any character in class \w?(e,g, space, tab, newline?)

      Not really, no. To illustrate the difference:

      my $string = 'run rabbit run'; print "Matched with word boundary\n" if $string =~ /\brun\b/; print "Matched with non-word char\n" if $string =~ /\Wrun\W/;

      Update: I (and the above code) assume you meant 'class \W' rather than 'class \w.' If you meant 'class \s', the answer is the same:

      print "Matched with whitespace\n" if $string =~ /\srun\s/;

      The \w character class does not include space, tab or newline.   The \w character class is comprised of letters, numbers and '_' the underscore character.

      Also, the \b word boundary does not match a character, it is a zero-width assertion, it matches between characters.