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

I cannot get an exact match for part of a search script function I created. My search function allows me to select a boxname, example, boxname1, it however finds results for not only boxname1 but also for boxname11, boxname12, boxname13...etc. How can I get it to only find results for boxname1 only? This is what I have for that part of the search. I use the caret ^ to anchor at the beginning and the $ to anchor at the end. #######
if ($srch_host) { $LookFor .= "^$srch_host\$"; } else { $LookFor .= '^[a-zA-Z_0-9]+?$'; }

Replies are listed 'Best First'.
Re: Regexp - exact matches
by BrowserUk (Patriarch) on Jul 30, 2003 at 18:35 UTC

    Without more to go on it is difficult to suggest much, but I think that you may be better served by the \b anchor than ^ and $.

    From your snippit, it looks like you are concatenating several pieces of regex together before using them. This suggests that you could well end up with a regex that has several occurances of the ^ and $ anchors, which probably isn't what you want.


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "When I'm working on a problem, I never think about beauty. I think only how to solve the problem. But when I have finished, if the solution is not beautiful, I know it is wrong." -Richard Buckminster Fuller

Re: Regexp - exact matches
by sauoq (Abbot) on Jul 30, 2003 at 18:24 UTC

    I'm also wondering why you are appending to $LookFor. If it is previously empty and you are using that as the pattern to match, that code should work. You probably don't want to append to it, just set it. Using qr// should help eliminate the escaping you've needed to do. And in that second regex, you can A) use \w which is a shortcut for the character class you've specified and B) eliminate the non-greedy matching as it doesn't help you because you've anchored your match anyway.

    if ( $srch_host ) { $LookFor = qr/^$srch_host$/; } else { $LookFor = qr/^\w+$/; }
    -sauoq
    "My two cents aren't worth a dime.";
    
Re: Regexp - exact matches
by dragonchild (Archbishop) on Jul 30, 2003 at 19:05 UTC
    I'm going to guess that your problem arises from two issues. Please note that this is a complete guess because I don't see enough of your code to say anything more. I could very well be completely wrong.
    1. $srch_host isn't being set or is mispelled. You probably need to add use strict; at the top of your script. That should catch a lot of spelling errors.
    2. $LookFor is being concatenated to. That looks really weird, seeing as you're specifying the complete match with ^ and $. I would assign to $LookFor and go from there.
    Of course, if you were to post a larger snippet, we would be able to see the issue(s) more clearly.

    ------
    We are the carpenters and bricklayers of the Information Age.

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: Regexp - exact matches
by kingstonian (Initiate) on Jul 30, 2003 at 19:40 UTC
    >>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
      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 ... }
Re: Regexp - exact matches
by The Mad Hatter (Priest) on Jul 30, 2003 at 18:10 UTC
    1. What is $LookFor and why are you appending to it?
    2. In the second line, you escape the $... Update Thanks isotope, missed that they were double quotes... ; )

    Please provide more of an explanation or more code.

      Looks to me like $LookFor is a string to be used as the regular expression. In the second line, he's building it with the contents of $srch_host as an exact match, using double quotes, so for the ending anchor $ to be included literally he has to escape it. That said, I don't think he's included enough of the code to find where the problem lies.

      --isotope