my $re = assemble_regex(\%hash); my $old_pos = pos; while (m/\G$re/){ $pos = $old_pos; # reset match position # ... extract name of matched regex in $matched_re here ... delete $hash{$matched_re}; # re-generate regex, without the one that previously matched: $re = assemble_regex(\%hash); }