admiralfreezbee has asked for the wisdom of the Perl Monks concerning the following question:

I have address like:
FD02:3539:700B:1::FE

the FD02:3539:700B and 1 are variable. How can i store them?

For now I only have this:
fd02:3539:700b:(\S+)::fe
but I want the rest too. The letters between the : are hexadecimal numbers

Replies are listed 'Best First'.
Re: Regular Expressions for IPv6 Addresses
by calin (Deacon) on Sep 28, 2005 at 11:28 UTC

    If you're dealing with IPv6 addresses check out the Net::IP module for bringing them to the canonical form and other operations.

    $ perl -l use Net::IP; my $ip = new Net::IP("FD02:3539:700B:1::FE"); print $ip->ip(); ^D fd02:3539:700b:0001:0000:0000:0000:00fe
Re: Regular Expressions for IPv6 Addresses
by holli (Abbot) on Sep 28, 2005 at 11:18 UTC
    my $v = "FD02:3539:700B:1::FE"; #way 1 if ( $v =~ /^([0-9A-F]{4}):([0-9A-F]{4}):([0-9A-F]{4}):([0-9A-F]{1}): +:([0-9A-F]{2})/ ) { print "$1 - $2 - $3 - $4 - $5\n"; } #way 2 my @v = split /::?/, $v; print join (" + ", @v), "\n";


    holli, /regexed monk/
      Thanks! That's it!
Re: Regular Expressions for IPv6 Addresses
by GrandFather (Saint) on Sep 28, 2005 at 11:22 UTC

    It may be that the following is what you want:

    use strict; use warnings; my $str = 'FD02:3539:700B:1::FE'; my ($p1, $p2, $p3, $p4) = split /:/, $str; print "$p1, $p2, $p3, $p4";

    prints:

    FD02, 3539, 700B, 1

    Perl is Huffman encoded by design.
Re: Regular Expressions for IPv6 Addresses
by ghenry (Vicar) on Sep 28, 2005 at 11:28 UTC

    Update: Ignore this, I missed that the OP was talking about a IPv6 address, not a MAC address.

    Have a look at:

    Regexp::Common::Net Mac address option

    HTH.

    Walking the road to enlightenment... I found a penguin and a camel on the way.....
    Fancy a yourname@perl.me.uk? Just ask!!!
Re: Regular Expressions for IPv6 Addresses
by prasadbabu (Prior) on Sep 28, 2005 at 11:18 UTC

    One way to do this,

    $t = 'FD02:3539:700B:1::FE'; if ($t =~ /^([^:]*):([^:]*):([^:]*):([^:]*)::([^:]*)/) { print "$1\t$2\t$3\t$4\t$5"; }

    This will match.

    updated

    Prasad

Re: Regular Expressions for IPv6 Addresses
by Anonymous Monk on Apr 30, 2008 at 07:00 UTC
    hello, can i get the regex in perl for fe80::21a:4dff:fe09:4a9a%4 i hav wriiten the code but its executin for othr ipv6 addrs my $value = `ipconfig`; print"$value\n"; if($value =~ /^Ethernet adapter Local Area Connection:(.)+IP Address\.\s+\s:+( (\w+\::)+((\w+\:){4})+\w+(\%\d+))/ms) { print ">>>>>>>> Ethernet IP Address $2 \n"; } plz help me out since i am a new to perl.. thankx in advance.