Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I generate all possible permutations from a Perl regular expression?

I know you can generate all permutations from a list, using glob or Algorithm::Permute for example - but how do you generate all possible permutations from a regular expression?

I want to do like:

@perms = permute( "/\s[A-Z][0-9][0-9]/" );
sub permute( $regex ) {
    # code - put all permutations of above regex in a list
    return @list;
}
like image 630
wibble Avatar asked Apr 16 '10 17:04

wibble


2 Answers

See Section 6.5 (PDF) in Higher Order Perl. Consider buying the print book: It is a work of art.

There is also Regexp::Genex on CPAN.

like image 144
Sinan Ünür Avatar answered Sep 19 '22 01:09

Sinan Ünür


Any possibly implementation should have a reasonable maximum length in mind for the generated strings. If there's a + or * anywhere in that regexp, the possibilities could be without end. Regexp::Genex considers this.

like image 26
dlamblin Avatar answered Sep 22 '22 01:09

dlamblin