#!/usr/bin/perl -l # https://perlmonks.org/?node_id=1226058 use strict; use warnings; $_ = 'aabbcc'; my $len = length; my $inner = join ',', split //, tr///csr; my $glob = "{$inner}" x $len; my $re = "^(?=.{$len}\$)"; while( /./ ) { my $count = s/$&//g; $re .= "(?=(?:.*$&){$count})"; } $_ =~ $re and print for glob $glob;