Is there a class that can generate a random regex string if a pattern is given?

I am getting an input string which is a Regex pattern that I need to randomly generate a string to match that pattern. I didn't know if it was about parsing the regex string piece by piece and using a random generator for each piece, or if there was a class that had this functionality out of the box.


Input string = "[0-9] [AZ]"

Possible outputs = "1A", "9B", etc.


source to share

2 answers

From what I could find Xeger is the most popular solution, but it is in Java.

However, there seems to be a C # version: Fare .
If you read the description, they say that Xeger is partially ported to the Fare app, but not completely.

Included a port of .NET Xeger for generating random text from regular expressions. Xeger does not support all valid Java regular expressions. The complete set of what is defined here and is summarized in ( ).

I haven't tested it and don't know if / how it works. So use as you see fit. I couldn't find anything else that was easy and ready to go, which looks like a break. You might think there would be some prepared class ...

If you know you need this for something relatively simple, then I would say that you are better off writing your own little RegexGenerator. This would entail parsing each piece of input " [..]

" and using a Random

string to create that piece.



If you can control the input, why not write it? I gave your two examples and added \d

as an option. You will need to develop all the combinations that only you know the user will provide.

User input

Say the user has:

var input = "[0-9][A-Z]";


These are two placeholder placeholders, one for a number and an uppercase letter.


So, let's create a placeholder class that is responsible for storing each of the elements, but it is also responsible for generating a random symbol. Here is the designed class

public class PlaceHolder
    public Random RND { get; set; } // Supplied random number generator

    public string Pattern { get; set; } // User Pattern
    public string Characters { get; set; } // Characters available to use
    public string ReplaceChar              // Generated random character based on pattern.
        get { return Characters[RND.Next( Characters.Length )].ToString(); }


Matching bookmarks

So, we set the capabilities of the generator placeholder as such:

Random rn = new Random();
var PlaceHolders = new List<PlaceHolder>()
 new PlaceHolder() { Pattern = "[A-Z]", Characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ",  RND=rn },
 new PlaceHolder() { Pattern = "[0-9]", Characters = "0123456789", RND=rn  },
 new PlaceHolder() { Pattern =  @"\d",  Characters = "0123456789", RND=rn  }



Now we will need to parse the user input to determine what random text is needed for the user input.

Before we can do that, we need to create a regex pattern based on all the placeholders we have. So, let's generate a pattern based on these, but avoid the regex special characters and put placeholders in parentheses ( )

so we can match them.

// Create pattern such as `[A-Z]|[0-9]|\d` but it escapes the regex specials
// to return `(\[A-Z])|(\[0-9])|(\\d)`
var pattern = 
      string.Join("|", PlaceHolders.Select (ph => Regex.Escape(ph.Pattern))
                                   .Select (pttrn => string.Format("({0})", pttrn)));


Create result

We will now analyze the input and project the matches found in Placehoders. When we have the correct placeholders, we generate random text against it.

            Regex.Matches(input, pattern)
                 .Select (mt => PlaceHolders.Find(ph => ph.Pattern == mt.Groups[0].Value) )
                 .Select (plc => plc.ReplaceChar))


Actual results



This is loosely based on my C # blog post : Generate a random sequence of numbers and letters from a custom template and symbols "OmegaMan Musings which I will have to update with this script."



All Articles