0

I have a quick question. What am I doing wrong in below code:

#!/usr/bin/perl -w
use strict;
my $num = "12345";
print "$num \n" if $num =~ m/\d{1,3}/;

As my number is 5 digits long, I was hoping that the output should not print anything because if statement is looking for a number with at least 1 digit and maximum 3 digits. But my script pints the output as below:

# perl num.pl 
12345 

Am I misunderstanding the above regex?

Thanks.

EDIT:

So ,Actually I am trying to match an IP address like string. e.g. i want to match 11.222.3.444 but it did not work with m/\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}/ so i started working on a single string containing only the number and hence then posted this on the stack overflow.

If i have to put ^ OR $ then how should i go about using them if I want to match IP address like string s mentioned above.

Thanks for you time.

slayedbylucifer
  • 22,878
  • 16
  • 94
  • 123

2 Answers2

2

What your perl code says is, "inside the string $num can you find a string at least 1 character at most 3 characters made only of numbers." The answer of course is yes.

What you want is

print "$num \n" if $num =~ m/^\d{1,3}$/;

Which means "inside the string $num starting at the first character and going to the last character is there a string at least 1 character long at most 3 characters long made only of numbers."

If you are trying to find something like an ip address regexp to match IP address is a good discussion of how to do that.

Michelle Six
  • 655
  • 3
  • 13
0

The below code validates the IP address correctly. The validations considered are listed below. 1) If the length of any part of the IP adress seperated by the decimal is more than one digit, then it should not start with zero. 2) The first of the IP adress seperated by the decimal cannot be zero. 3) Any part of the IP adress seperated by the decimal cannot be more than 255.

Request the members to fine tune the regex as it look too big and confusing.

print "Enter the IP address : ";
$ip = <stdin>;
if($ip =~ /^([3-9]\d?|1\d?\d?|2([0-4]?\d?|5[0-5]|[6-9]?))(\.(0|[3-9]\d?|(1\d?\d?|2([0-4]?\d?|5[0-5]|[6-9]?)))){3}$/) {
    print "Correct\n";
}
else {
    print "Wrong\n";
}
Kamleein
  • 11
  • 2
  • Did you try it? This matches `9558.7506.5556.4545` for example. – Toto Mar 26 '14 at 12:53
  • Yes I did try it.... below is the result that I get while executing the code with your input value. $ perl ip.pl Enter the IP address : 9558.7506.5556.4545 Wrong – Kamleein Mar 26 '14 at 12:58
  • Yes, you're right for this one but it validates `955.955.955.955`. – Toto Mar 26 '14 at 13:05
  • Hi M42, Thanks for your feed back. Well I have modified the regex above. Now it seems good. Will be happy to enhance it if further comments are received. – Kamleein Mar 26 '14 at 13:23
  • The regex has been modified. Please do let me know if it is helpful. Experts please do review and let me know how this can me made more understandable. – Kamleein Mar 26 '14 at 14:50
  • @M42 - Have modified the regex with reference to the provided link, thanks. The issue with the provided link is that the IP address 1.1.1.01 is invalid but this is successfully matched in that link. – Kamleein Mar 26 '14 at 16:09