I have a string aa:bb::cc:yy:zz
which needs to be split in such a way that I have an array with aa:bb::cc
, yy
, zz
. i.e. I want to create two substrings from last with :
as delimiter and remaining as a element of an array. What's the best way to achieve this?
ex:
aa:bb::cc:yy:zz --> ['aa:bb::cc','yy','zz']
dd:ff:gg:dd:ee:ff:fg --> ['dd:ff:gg:dd:ee','ff','gg']
I store IP address:port:protocol as a key in a file , and splitting wiht ":" to get IP,port,proto back and things were working fine when IP address is limited to Ipv4. Now I want to make it ported to Ipv6 in which case IP address contains ":" so I can't get proper IP address by splitting with ":".
How about:
#!/usr/local/bin/perl
use Data::Dump qw(dump);
use strict;
use warnings;
my $x = 'dd:ff:gg:dd:ee:ff:fg';
my @l = $x =~ /^(.*?):([^:]+):([^:]+)$/g;
dump @l;
output:
("dd:ff:gg:dd:ee", "ff", "fg")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With