Pregunta

I wanted to read a string in Perl but i can't seems to get it right. The string is :

udp://10.111.80.95:8081

From this string, i want to only read the ip address part only which is :

10.111.80.95

And also i wanted to read the port part only which is :

8081

So with this i have 2 values that i can read which is the ip address and the port.

How can i achieve this? Do i need to use regex? But my regex had never get this right. Can anybody help me with this? Thank you.

¿Fue útil?

Solución

There is no need to use regular expression for this needs, split is enough. You could split your string at / and :, then the third field is ip address and fourth field port number.

#!/usr/bin/perl

use strict;
use warnings;

use feature qw(switch say);

use Data::Dumper;

while (<DATA>) {
    chomp;
    my @field = split /\/|:/;
    print "$field[3] $field[4]\n";
}

__DATA__
udp://10.111.80.95:8081

Running:

$ perl t.pl
10.111.80.95 8081
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top