How to generate random strings that match a given regexp?

前端 未结 5 684
花落未央
花落未央 2020-12-08 08:10

Duplicate:

Random string that matches a regexp

No, it isn\'t. I\'m looking for an easy and universal method, one that I could actu

5条回答
  •  北荒
    北荒 (楼主)
    2020-12-08 08:39

    The universality criterion is impossible. Given the regular expression "^To be, or not to be -- that is the question:$", there will not be ten unique random strings that match.

    For non-degenerate cases:

    moonshadow's link to Perl's String::Random is the answer. A Perl program that reads a RegEx from stdin and writes the output from ten invocations of String::Random to stdout is trivial. Compile it to either a Windows or Unix exe with Perl2exe and invoke it from PHP, Python, or whatever.

    Also see Random Text generator based on regex

提交回复
热议问题