asiufy has asked for the wisdom of the Perl Monks concerning the following question:

Monks,

Is there any easy way to generate a string (letters/numbers) from regexp/character class?

Thanks.
  • Comment on Generating string that conforms to a regexp

Replies are listed 'Best First'.
Re: Generating string that conforms to a regexp
by blakem (Monsignor) on Feb 01, 2002 at 22:03 UTC
      I am sorry, I should've been more specific...

      It seems like I want the opposite of what was discussed in that thread...

      Given :

      /^(\w){3}(\d){5}(\w){3,5}?/

      I need to generate x number of strings based on that given regexp, such as :

      ABC00000ABC
      ABC01231ABCD
      AAA11231BDBDB

      The letters/numbers can be random.
        This looks fun. And since it appears that you are proposing to use a rather limited subset of regex possibilities, not too hard.

        Here's a working roughed-out beginning toward a generic solution for a certain subset of regex possibilities along the lines you describe.

        #!/usr/bin/perl -w use strict; sub randchrs { my ($type, $length) = @_; $length ||= '1'; my @w = ('A'..'Z', 'a'..'z', '0'..'9', '_'); my @d = ('0'..'9'); my @chrs; for ($type) { /w/ && do {@chrs = @w; last}; /d/ && do {@chrs = @d; last}; # other options here } if ( $length =~ /(\d+),(\d+)/ ) { $length = $1 + int(rand($2 - $1 + 1)); } my $answer = ''; for (1..$length) { $answer .= $chrs[int(rand(@chrs))]; } return $answer; } while ( my $regex = <DATA> ) { chomp $regex; $regex =~ s/\(?\\([wd])\)?({([\d,]+)})?/randchrs($1,$3)/eg; print "$regex\n"; } __DATA__ STRING: (\w){3}(\d){5}(\w){3,5} PHONE: \d{3}-\d{3}-\d{4} SSN: \d{3}-\d{4}-\d{3} MORE: several word chars: \w{3,8} WA_DL: \w{7}\d{3}\w{2} STUFF: \w\d\w\d\w\d ANGRY: "\w{5,9}", he said, "\w{5,9}, \w{5,9}, and \w{5,9}."
        That might serve to get you started.

        Note in passing: your description of the problem is a bit broader than the solution examples you offer, so you may need to tweek things accordingly. You will certainly need to tweek this example anyway.

        Grins, David

        ------------------------------------------------------------
        "Perl is a mess and that's good because the
        problem space is also a mess.
        " - Larry Wall

(tye)Re: Generating string that conforms to a regexp
by tye (Sage) on Feb 01, 2002 at 23:25 UTC

    That's a bit vague, but you might find this previous discussion helpful.

    -tye (I am not "Blake")
    Actually, I didn't think the question was vague. ;)
      ++tye!

      excellent example of laziness! not only do you reuse code, you reuse posts!

      does the "artistic license" cover that?

      ~Particle

Re: Generating string that conforms to a regexp
by busunsl (Vicar) on Feb 05, 2002 at 09:48 UTC
    There is a module called String::Random. It has a function randregex which might do what you want.
      I have looked at that module, and while it seems pretty good, its regexp capabilities are way too limited, and it hasn't been updated in almost 2 years ... I will take some time to go over the module's code to see if I can do something...
Re: Generating string that conforms to a regexp
by demerphq (Chancellor) on Feb 04, 2002 at 11:03 UTC
    I wrote a set of modules to do just this.

    They are in an experimental state so I havent published them here or on CPAN.

    If you would like a copy email me at the address posted on my home node.

    But they should do what you want.

    Yves / DeMerphq
    --
    When to use Prototypes?