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

O.K. Perl Monks, Lets say I have a file called myfilename.html that looks like this:

<!-- This is an example file --> <p>this is a link to a book called<br> <a href="http://www.foobar.tv/booklink.html"> <hr>etc etc

I wrote a small script that tells me which lines match a certain sting I enter on input, as in:
#>perl filematch.pl book

$matchtext = shift; $page = "myfilename.html"; open PAGE, $page; @page_text = <PAGE>; close PAGE; foreach $page_line (@page_text) { if ($page_line =~ m/$matchtext/) { print "There was an instance of $matchtext\n"; print "on this line:\n"; print "$page_line\n"; } }

The code above will tell me there is a match when $matchtext = "book", but it won't match when $matchtext = "booklink". I thought that even though, in my file, "booklink" is surrounded by non-whitespace, it should match nonetheless...

Anybody know what's wrong?

Replies are listed 'Best First'.
(Ovid) Re: Simple REG/EX Question
by Ovid (Cardinal) on Feb 25, 2002 at 22:10 UTC

    I'm not sure what's going on. I tested it and it works for me. I suspect that something changed from when you ran it and when you posted it. Incidentally, here's something a bit closer to how I would write that:

    use strict; my $matchtext = shift || die &usage; if ( ! is_valid_pattern( $matchtext ) ) { die "'$matchtext' is not a valid regular expression."; } my $page = "myfilename.html"; open PAGE, "< $page" or die "Cannot open $page for reading: $!"; while (<PAGE>) { print "There was an instance of $matchtext on line $.:\n\t$_\n" if + /$matchtext/; } close PAGE; sub usage { print "Usage: filematch.pl text_to_match"; } sub is_valid_pattern { my $pattern = shift; return eval { "" =~ /$pattern/; 1 } || 0; }

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

Re: Simple REG/EX Question
by screamingeagle (Curate) on Feb 25, 2002 at 22:08 UTC
    hi,
    i just ran the code u had posted and this is the output i got :
    There was an instance of book on this line: <p>this is a link to a book called<br> There was an instance of book on this line: <a href="http://www.foobar.tv/booklink.html">
    so it looks like it is doing what you want it to do...(or am i missing something ?)
Re: Simple REG/EX Question
by YuckFoo (Abbot) on Feb 25, 2002 at 22:17 UTC
    No special regex tricks here, 'booklink' should match just as easily as 'book'. But what's gonna happen if you fail to open your file? Maybe you should:

    open(PAGE, $page) || die $!;

    Just in case.

    YuckFoo