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

Hi, i'm relativly new to Perl and am having trouble getting a simple match to work. I am setting up a site search for a website and my script takes in a searchword and uses it in an exact matching statement with the other html pages on the site. i am loading each HTML page into a variable like this "$content = <INPUT>;" and then matching like this... "if($content =~ m/^$searchword$/)" ($searchword is the variable where the users search is stored) This is not working and i don't know why?? All i want it to do is if a user searches for "page", they do not get matches like "pa" or "pag". Only the exact string. Any help would be greatly appreciated. Thanks. Thanks guys...problem solved.

Replies are listed 'Best First'.
Re: Matching problem
by Happy-the-monk (Canon) on Jul 08, 2004 at 08:52 UTC

    Try:

    chomp( my $content = <INPUT> ); # to get rid of a trailing linebreak.
    if ( $content =~ m/^\Q$searchword\E$/ ) # escape/quote funny characters in $searchword.

    Update:
    The anchors   ^   and   $   match beginning and end of the string. I understand that's what you want.
    If you want to match the word inside a longer string, go for ccn's proposal using the word boundary   \b   instead of the anchors.
    See either   perldoc perlrequick, perldoc perlre or perldoc perlretut

    Cheers, Sören

Re: Matching problem
by ccn (Vicar) on Jul 08, 2004 at 09:00 UTC
    m/$searchword/
    or
    m/\b$searchword\b/


    see also:
    perldoc perlre
Re: Matching problem
by elbow (Scribe) on Jul 08, 2004 at 09:51 UTC
    If you are opening the html file and reading into a scalar i.e.
    use strict; my $input = "somefile.html"; open (INPUT, $input) || die; my $content = <INPUT>; close INPUT;
    it will just pick up the first line in the file. Instead you want to either a) read the file into an array, or b) use LWP::Simple to get the page into a scalar i.e.
    use strict; my $content = LWP::Simple::get($input);

    elbow
Re: Matching problem
by ercparker (Hermit) on Jul 08, 2004 at 14:08 UTC
    if you dont want to loop through file line by line then you could also do this:
    open (FILE, "<file.html") || die "couldn't open file: $!\n"; my @data = <FILE>; my $file = join /\n/, @data; if ($file =~ m{$searchword}g) { #do something }
      Rather than making two copies of the full file content in memory (and wasting cycles on a useless join), it would be better either to slurp the whole file into a scalar, like this:
      { local $/ = undef; open(FILE,$filename) or die "open failed on $filename: $!"; $wholetext = <FILE>; close FILE; } if ( $wholetext =~ /\b$searchword\b/ ) { # do something }
      Or to read the file into an array and use grep on the array, like this:
      open( FILE, $filename ) or die "yadda yadda"; @data = <FILE>; if ( grep /\b$searchword\b/, @data ) { # do something; }