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

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

I want to do as:

@perms = permute( "/\s[AZ][0-9][0-9]/" ); sub permute( $regex ) { # code - put all permutations of above regex in a list return @list; } 
+6
regex perl permutation
source share
4 answers

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

There is also Regexp :: Genex on CPAN .

+6
source share

Any possible implementation should have a reasonable maximum length for the generated lines. If there is a + or * in this regular expression, the possibilities can be endless. Regexp :: Genex considers this.

+2
source share

None of the solutions I came across look with handles; Regexp :: Genex does not and does not make this decision here:

http://www.mail-archive.com/ spamassassin-talk@lists.sourceforge.net /msg31051.html

Although I agree that HOP is a terrific book, it really only deals with a small subset of the regular out-of-box expressions.

If anyone knows what lookaheads handles, that would be great: /

0
source share

just in case anyone finds this useful:

  * $ cat bitfizz.pl *
 #! / usr / bin / perl 
 use strict;
 if (($ # ARGV + 1)! = 2) {print "usage $ 0 \ n";  }
 my @r = & bitfizz ($ ARGV [0], $ ARGV [1]);
 for (@r) {print "$ _ \ n";  }
 sub bitfizz () {
     $ _ [0] = join (",", split (//, $ _ [0]));
     for (my $ i = 1; $ i & lt = $ _ [1]; $ i + = 1) {$ _ = $ _. "{$ _ [0]}";  } 
     @ r = glob ($ _);
 }

then you can do:

  * perl bitfizz.pl "01" 8 *
 00000000
 00000001
 00000010
 00000011
 00000100
 --snip--

all byte permutations in bits or

  * perl bitfizz.pl "0123456789ABCDEF" 2 *

eg

0
source share

All Articles