×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

String match issue
2

String match issue

String match issue

(OP)
Please take a look at the small sample code here.
I just don't understand why it actually finds a match!!

CODE

#!/usr/bin/perl -w

my $str = "group=1,101,102";
my $ip = "1.1.1.1";
if($str =~ /$ip/) {
  print "The string contains this IP!!\n"
}
else {
  print "The string does not contain this IP!!\n"
}
print "String: #$str#\n";
print "    IP: #$ip#\n";
exit; 

Sample run:

CODE

./test.pl 
The string contains this IP!!
String: #group=1,101,102#
    IP: #1.1.1.1# 

Thanks for your time and help!!

RE: String match issue

The periods need to be escaped. Right now, they are matching any character (which includes , 0 ,). Try it with this:

CODE

#!/usr/bin/perl -w

my $str = "group=1,101,102";
my $ip = '1\.1\.1\.1';
if($str =~ /$ip/) {
  print "The string contains this IP!!\n"
}
else {
  print "The string does not contain this IP!!\n"
}
print "String: #$str#\n";
print "    IP: #$ip#\n";
exit; 
Note, i have switched the double quotes to single quotes, so I did not have to escape the escape character. I have not tested this, so feel free to come back with problems.

RE: String match issue

The dot stands for 'any character' in the regex. One way to solve this is to disable the metacharacters by prepending \Q : if($str =~ /\Q$ip/) {

http://www.xcalcs.com : Online engineering calculations
http://www.megamag.it : Magnetic brakes for fun rides
http://www.levitans.com : Air bearing pads

RE: String match issue

Use \Q...\E to escape regular expression special characters, like the any character '.'

Also use word boundaries \b so that you don't match a substring like 1.1.1.100

CODE

use strict;
use warnings;

my $str = "group=1,101,102";
my $ip = "1.1.1.1";

if ($str =~ /\b\Q$ip\E\b/) {
	print "The string contains this IP!!\n"

} else {
	print "The string does not contain this IP!!\n"
}

print "String: #$str#\n";
print "    IP: #$ip#\n";
exit; 

- Miller

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close