That would require travelling the regex node tree. It doesn't seem very possible, even with reading the results of re.pm output... I've thought about making a text-to-regex translator, but I stop everytime I think about it, since that would require parsing Perl in Perl, which is not a very nice idea. With simple regexes like yours, though, you can hack it together yourself. I honestly have no idea why I wrote this. It is far too much for such a simple problem.
package MatchRegex; use strict; use vars qw( @ISA @EXPORT_OK @digits @alpha ); require Exporter; @ISA = 'Exporter'; @EXPORT_OK = qw( digits alpha UCalpha LCalpha ); @digits = (0..9); @alpha = ('a'..'z', 'A'..'Z'); sub digits { ref $_[0] and local @digits = @{shift()}; # could use my() my $len; if (!@_) { $len = 1 } elsif (@_ == 1) { $len = $_[0] } else { $len = $_[0] + rand($_[1] + 1 - $_[0]) } return map $digits[rand @digits], 1 .. $len; } sub LCalpha { local @alpha = @alpha[0..25]; α } sub UCalpha { local @alpha = @alpha[26..51]; α } sub alpha { ref $_[0] and local @alpha = @{shift()}; # could use my() my $len; if (!@_) { $len = 1 } elsif (@_ == 1) { $len = $_[0] } else { $len = $_[0] + rand($_[1] + 1 - $_[0]) } return map $alpha[rand @alpha], 1 .. $len; }
And then:
use MatchRegex qw( digits UCalpha ); $filename = join "", ( digits(2), # \d{2} UCalpha(2,5), # [A-Z]{2,5} '_', # _ digits(2), # \d{2} digits(2), # \d{2} digits(4), # \d{4} );
This is a glorified "make a random string as per my request" module. It can be expanded. But it can't interpret your regex for you. That's left up to you. Maybe MJD or Ilya or someone else could help hack at the regex engine to make something like this more likely.

$_="goto+F.print+chop;\n=yhpaj";F1:eval

In reply to Re: Bidirectional Regular Expression Parsing and Matching Text Generation? by japhy
in thread Bidirectional Regular Expression Parsing and Matching Text Generation? by princepawn

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.