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