in reply to Re^2: search for exact word only
in thread search for exact word only

The solution I provided solves the problem you describe here. It matches on word boundaries, it does not require the entire string to match. My solution did not include strict, warnings, or Carp, but strict and warnings are pragmas, and do not require external modules. Warnings is not supported in some versions of Perl, so you should rely on the -w option, in that case.

Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^4: search for exact word only
by Anonymous Monk on Sep 27, 2004 at 18:08 UTC
    Could you put the lines you provided inside my code? I got '500 Internal Server Error'. I suspect, that probably I have incorrectly closed braces or not in the right place.

    Thanks
    tenerif

      You can just use your original code, and where you did have the pattern
      /\Q$key/
      replace it with
      /\b\Q$key\E\b/

      Caution: Contents may have been coded under pressure.
        Yes, this works.

        thank you for help,
        tenerif
        I would like determine why this code not worked, its possible,I has incorrectly inserted brace.
        if($action eq '!='){ $match{$a} = 0 if $line[$position]=~m/\Q$key/i; } else{$match{$a} = 1 if $line[$position]=~m/\Q$key/i;}

        Can be rewritten like so: $match{$a} = ($line[$position] =~ m/\b\Q$key\E\b/i) ? 1 : 0; $match{$a} = !$match{$a} if $action eq '!=';
        Is this correct? Please correct me if something wrong:
        if($action eq '!='){ $match{$a} = ($line[$position] =~ m/\b\Q$key\E\b/i +) ? 1 : 0; } else{$match{$a} = !$match{$a} if $action eq '!=';}