((?:\[.*?\]){1,4}) #### my @indexes; my $re = qr{ ... # everything before (?: \[ # opening [ ([^\]]+) # everthing except ] \] # closing ] (?{ push @indexes, $^R}) # store the match )+ # as many times as you want }x #### re: (..) (..)+ vars: $1 $2