Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pattern matching -- exact match

Status
Not open for further replies.

skw

Instructor
Jun 1, 1999
19
US
I'm tearing my hair out on this one. I've searched through tek-tips but could not find a distinct answer for my question.

my search function script matches and finds a string, but I want to make the string search to be an *exact match*.

If I type in mybox1, it also finds mybox14, but I only want it to find 'mybox1'

I have this
if ($srch_host) {
$LookFor .= "\.$srch_hostHost";
}
else {
$LookFor .= '\.\w+?';
}

Thanks for the help.
 
For an exact match define the beginning (^) and end ($) of the line. Here's an example:
Code:
@Strings = ("abc1234", "1abc123", "abc123");
$Match = "abc123";
foreach $String (@Strings) {
    if ($String =~ m|^$Match$|) { # another way if ($String eq $Match) {
       print "String '$String' is an exact match for '$Match' \n";
    }
}
 
Why use the regular expression engine at all? The [tt]eq[/tt] operator is made for the job.

Yours,


fish

"As soon as we started programming, we found to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs."
--Maurice Wilkes
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top