in reply to Regexp - exact matches

>>Jamaica responding<< The $LookFor is a string to be used as the regexp. Yes, it is the pattern to match. I've tried using \b but did not get any results. If I leave off the ^ and $, my search goes thru but it include other matches, which I don't want. I'm trying to do a search on my log files, and a typical filename would be fileis.2003.07.box1 or fileis.2003.07.box11 I tried using qr// but it too did not work. Yes I use strict; and yes, I verified that all the string/variable names are spelled correctly. Thanks for the help everyone. I need it! So this is what I have... (this isn't all of it but this should give you basic idea w/out me having to dump everything here)
if ($srch_file) { $LookFor .= "$^srch_file.*?"; } else { $LookFor .= '^[A-Za-z]+?'; } if ($srch_year) { $LookFor .= "\.$srch_year"; } else { $LookFor .= '\.\d+?'; } if ($srch_month) { $LookFor .= "\.$srch_month"; } else { $LookFor .= '\.\w+?'; } if ($srch_host) { $LookFor .= "\.$srch_host"; } else { $LookFor .= '\.\w+?'; } print "Looking in:\n"; #look in the specified dir paths set in th +e search_dirs foreach (@Search_Dirs) { print "\t$_/\n"; #print out dir paths that it searched } print "For file names matching: /$LookFor/\n"; #print out the file + names that matches

Replies are listed 'Best First'.
Re: Re: Regexp - exact matches
by graff (Chancellor) on Jul 30, 2003 at 23:52 UTC
    You appear to be printing out the final value of "$LookFor" as a confirmation/debugging message, prior to actually doing the search. So the big question is: what shows up as the value of "$LookFor"? Given the code and sample file names you've given us just now, the last bit we're missing is knowing the values of the various "$srch_XXX" strings -- maybe this can be guessed at if we know what shows up as the final value of "$LookFor", and it will be possible to work out why it's giving you results you don't want.

    Apart from that, looking at what you have above, and assuming that "$srch_host" always has a literal-match string (like "box1", "box12", etc), then the part where you use "$LookFor" in a regex (not in your snippet) ought to work if you use it like this:

    if( /^$LookFor$/ ) { # true if $_ contains a matching file name ... }