3

I want to generate an example of a valid input by Regex pattern. I'm programming with C# .Net . Like this:

//this emthod doesn't exists, its an example of funcionality that I want.
Regex.GenerateInputExample("^[0-9]{15}$"); 

So, this example gives-me a possible value, like 000000000000000. How to do this?

8
  • 1
    There is no built-in functionality for this. You should analyze regex pattern and build sample input manually. It might be easy with simple patters like you have shown ^[0-9]{15}$, but something like this ^(?:(?=.*[a-z])(?:(?=.*[A-Z])(?=.*[\d\W])|(?=.*\W)(?=.*\d))|(?=.*\W)(?=.*[A-Z])(?=.*\d)).{8,}$ will require lot of work. I think building such analyzer and generator makes this question too broad. Commented Jun 11, 2014 at 20:59
  • 1
    There are 10^15 possibilities for the above Regex. How would you decide which one to choose? Commented Jun 11, 2014 at 21:01
  • 1
    According to computation theory, you can write a regular grammar for any regular expression, and regular grammars are producers (while regex are recognizers). Not sure how it would be used tho. Commented Jun 11, 2014 at 21:04
  • Expanding on @Mephy regex can be converted to push-down automaton etc... which I assume can then be converted into a regular grammar? Commented Jun 11, 2014 at 21:05
  • 1
    Have you taken a look at Xeger ? Commented Jun 11, 2014 at 22:21

1 Answer 1

0

So, this problem would take some time to solve, since the functionality is not built in. I'll give a general way to solve it:

Using an ascii (or unicode) chart find out the character codes that correspond to the characters you are using for your regex (65 =A, 69 = D, etc)

Create a random function with those bounds. Multiple bounds would take a little more trickery (A-Z =26, 0-9 = 10, so a random number from 0- 35)


Random random = new Random();
int randomNumber = random.Next(65, 70); // this generates a random number including the bounds of 65-69)

char temp = (char)random;

Next you would take the randomly generated characters and add them together into a string.

        int lowerBound = 65, upperBound =69;
        int length = 6;
        char temp;
        int randomNumber;
        string result= "";

        Random rand = new Random();
        for (int a = 0; a <= length; a++)
        {
            randomNumber = rand.Next(lowerBound, upperBound);
            temp = (char)randomNumber;
            result = result + temp;
        }                  //result is the indirect regex generated string

Indirectly giving you a regex generated string.

The next step is parsing information out of a regex. I've provided a simple case below that will not work for every regex, due to regex complexity.

        Regex bob = new Regex("[A-Z]");

        int lowerBound = Convert.ToInt32(bob.ToString()[1]);
        int upperBound = Convert.ToInt32(bob.ToString()[3]);
        int length = 6; //length of the string to be generated
        char temp;
        int randomNumber;
        string result= "";

        Random rand = new Random();
        for (int a = 0; a <= length; a++)
        {
            randomNumber = rand.Next(lowerBound, upperBound);
            temp = (char)randomNumber;
            result = result + temp;
        }

( This process could be streamlined into class and utilized etc)

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the answer hobble! But the regex in the question is only a example, in another regex I can use only characters, or only special characters, etc.. I dear something that works for everyone

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.