#!/usr/bin/perl -- use strict; use warnings; use 5.010; use Data::Dump; my $re = qr{ (?(DEFINE) (? 2[0-4]\d | 25[0-5] | 1\d\d | [1-9]?\d ) ) # match this ( # save to $1 \b (?&byte) ( \. (?&byte) ){3} \b ) }x; my $str = ' localhost 127.66.66.66 '; while( $str =~ m/$re/g ){ dd( [ $1, \%+, \%-, \%/ ] );#/ } dd([ $str =~ m/$re/g ] ); use Tie::Hash::NamedCapture; tie my %hash, "Tie::Hash::NamedCapture", all => 1; if( $str =~ m/$re/g ){ dd([ $1, \%hash ]); } __END__ [ undef, { # tied Tie::Hash::NamedCapture }, { # tied Tie::Hash::NamedCapture byte => [undef], }, {}, ] [undef, "127.66.66.66", ".66"] [ undef, { # tied Tie::Hash::NamedCapture byte => [undef], }, ]