Some solutions:
(1) If the regex is written by you (not by the user) and rarely changes, why create anything programmatically? You could just create a few nice examples by hand.
(2) Use a ready-made solution. (see other answers)
(3) Rejection sampling, the sledge hammer solution to all random generation problems: Create a random string and check if it matches the regex. If not, try again. If the regex is very specific, this solution has terrible performance, though.
(4) Implement a parser that transforms a regex into a string construction tree that consists e.g. of the nodes below. Every node has a CreateRandomString
method that follows certain rules. Creating a random string means calling that method for the root node.
concatenation: Traverse all child subtrees and concatenate the results in order.
random choice: Select a random child subtree and traverse it. Return the result.
multiplication: Create a random number n between a and b. Traverse the subtree n times and concatenate the results.
leaf: Return a constant string.
Creating the parser is the tricky part :) , especially nested structures. (I have written one for a syntax similar to regexes.)