Brad Baxter wrote:
> luke devon wrote:
>> Thanks for every one who tried to help me. but all were unsuccessful and I 
>> would like to submit my tries for your consideration. 
>>
>> This is how its done.
>> $ip = substr($ip, 0, (length($ip)-2));
> 
> (Please put your comments below others'.)
> 
> If that's how it's done, then you haven't given us very
> good examples.
> 
> 
> # I am storing IP in to a varable ,  $ip="172.22.8.10 \-";
> 
> {
>      my $ip="172.22.8.10 \\-";  # that '\' has to be escaped
>      $ip =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/;
>      $ip = $1;
> 
>      # prints: [172.22.8.10], i.e., it works fine
>      print "[$ip]\n";
> }
> 
> # I am receiving IP value as 192.168.10.5/ -
> 
> {
>      my $ip="192.168.10.5/ -";
>      $ip =~ /(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/;
>      $ip = $1;
> 
>      # prints: [192.168.10.5], i.e., it works fine
>      print "[$ip]\n";
> }
> 
> # This is how its done.
> # $ip = substr($ip, 0, (length($ip)-2));
> 
> for my $input (
>      "172.22.8.10 \-",  # that's really just "172.22.8.10 -"
>      "172.22.8.10 \\-",
>      "192.168.10.5/ -"
>      ) {
>      my $ip = substr($input, 0, (length($input)-2));
> 
>      # prints:
>      # [172.22.8.10] [172.22.8.10 ] [192.168.10.5/]
>      # The first one looks correct, because your input is faulty
>      # The other two are not correct
>      print "[$ip] ";
> }
> 
> __END__
> [172.22.8.10]
> [192.168.10.5]
> [172.22.8.10] [172.22.8.10 ] [192.168.10.5/]

(Nice to find a man who can use apostrophes :)

I would write it as below, which works for all of these cases. I don't think
there's any need to validate the IP address: it's simply a matter of finding it
in the string.

Rob



use strict;
use warnings;

my $ip = '"172.22.8.10 \-';

($ip) = $ip =~ /[0-9.]+/g;

print "|$ip|\n";

**OUTPUT**

|172.22.8.10|

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to