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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Making 5.1.1 not match 58180

Status
Not open for further replies.

inspleak

Technical User
Mar 10, 2004
14
US
Hey guys.. one more quick question and I promise I will leave you alone for a bit :)

Essentially all I want to do is match the string "5.1.1".
However I have noticed that PERL treats the '.' as a special character and '\.' doesn't seem to change it. The following code gets a match for "5.1.1" and "58131". So, how do I get 5.1.1 to only match 5.1.1 and not 58131?

Code:
@testArray = ("5\.1\.1",);

$testme = "58131";

$currentThing = $testArray[0];

if($testme =~ /$currentThing/i)
{
print "It Matches!\n";
}
else
{
print "No Match!\n";
}

Thank you so much for your help, again.
 
1. Enclose it in single quotes instead of double: '5\.1\.1'
or
2. Double the backslashes: "5\\.1\\.1"
or
3. Do a simple string comparison instead of an re match and
forget about backslashes, e.g.
Code:
@testArray = ("5.1.1",);
$testme = "58131";
$currentThing = $testArray[0];

if($testme [b]eq[/b] $currentThing) {
    print "equal!\n";
} else {
    print "not equal!\n";
}
 
That worked great! Thanks so much!
This leads me to one more question then... why two '\' characters? Why doesn't one work by it self?

Thanks again!
 
I don't know why. This is just something I discovered by trial and error myself. To get a single backslash into a double-quoted string, you need 2 backslashes. In a single-quoted string, you only need one.
 
The correct way to quote a regular expression for use later is to use qr//:
Code:
@testArray = (qr/5\.1\.1/);

As for the double backslash, the easiest way to see this in action is just to print a double-quoted string containing backslashes:
Code:
print "5\.1\.1\n";

That will print 5.1.1, which is what will get passed to the regexp engine in your example. To get the desired effect (i.e. a literal backslash character), you'd have to escape the backslashes. Now you can see why it'd match 58131. (Incidentally, it is this behaviour that makes regular expressions in Java a severe pain in the behind).
 
Thanks, ishnid. I don't think qr// was available when I started using Perl (or perhaps I just didn't know about it), so I got in the habit of using single quotes. I'll have to make myself start using qr//.
 
ahh.. I see.. thanks for the explanation! It really does make sense..
 
might be a silly question but are you trying to analyse the version of Perl?

if ($^V eq v5.6.1) {
print "accept\n";
} else {
print "reject...\n";
}



Kind Regards
Duncan
 
Nope... but thanks for the info... Didn't know you could get the version that way.. pretty cool.

Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top