You want the list of strings which may match a regex.
/^ab?(c|d)$/ => qw( abc abd ac ad )
This isn't reusable code, just a trick I found interesting and worth exploring. See the discussion (and code!) at Regexp generating strings? for some alternatives.

The idea is to (manually) replace the atoms with code assertions, building up a hyotheical match, printing it and then failing in order to backtrack and try again.

Brad

Update: Regexp::Genex

The code for /^ab?(c|d)$/ is
perl -e '""=~/(?{a})(?{$^R.b})?((?{ $^R.c})|(?{ $^R.d}))(?{print "$^R\ +n"})(?=a)(?!a)/' # output: abc abd ac ad
A more complex example follows, first some notes.

The above will produce a warning "Quantifier unexpected on zero-length expression..." under -w because of the (?{$^R.b})?. Suppress it with "no warnings 'regexp';"

The $^R variable contains the result of the last (?{}) assert and is localized for backtracking. (My first version had (?{ local $_ = $_ . "b" }) instead)

I tried the more complex /reg(ular\s+)?exp?(ression)?/ but ran into a few interesting, but so far surmountable problems.

Firstly, I haven't tried to tackle classes such as \s yet. I changed it to '_'. (YAPE::Regex has expanded classes)

I couldn't convince perl to repeatedly run zero-width assertions at the same position in the string. To see why search for "infinite" in perlre :). (If anyone knows how, let me know). To work around I made a string of 'a's and tacked an 'a' onto each quantified assertion. A happy side-effect is that the length of the input string bounds the quantifiers (somehow, but I ain't doing the math).

Also, the '*' quantifier is apparently optimized to not try to repeatedly match in the same position. I use {0,3} instead, as it isn't optimized. perlre again.

Greediness controls the search/display order.

I'm looking for a good impossible assertion to put at the end. Currently, (?!a)(?=a) since the obvious 'x' is also optimized. perl -Mre=debug tells why:
floating `x' at 0..2147483647 (checking floating)

I might be able to avoid the 'a's with m/.../g and resetting pos. I haven't investigated, but I doubt it.

#!/usr/bin/perl -w #orig: reg(ular\s+)?exp?(ression)?!*? #mod: ^reg(ular_+)?exp?(ression)?!{0,3}? no warnings 'regexp'; # zero-length + quantifier 'aa' =~ /^ (?{'reg'}) ( (?{ $^R.'ular'}) (?: (?{ $^R."_" }) a )+ )? (?{ $^R.'ex'}) (?{ $^R.'p'})? ((?{ $^R.'ression'}))? (?: (?{ $^R."!" }) a ){0,3}? (?{print "$^R\n"; }) (?=a)(?!a) # fail /x; __END__
Output
$ ./genex.pl regular__expression regular__exp regular__exression regular__ex regular_expression regular_expression! regular_exp regular_exp! regular_exression regular_exression! regular_ex regular_ex! regexpression regexpression! regexpression!! regexp regexp! regexp!! regexression regexression! regexression!! regex regex! regex!!
</readmore

In reply to Generating regex strings with a regex by bsb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.