9

I use regular expressions to validate user input. Now I can configure the regex and so it would help the user to see an example of how a certaint input has to be formatted.

Is it possible to generate some strings that match an arbitrary regex? And is there even an implementation usable somewhere?

UPDATE: Due to the licence I can not use REX. Are there other possiblities?

4
  • 1
    I suggest looking into : research.microsoft.com/en-us/projects/rex , they do such a thing. let me know if it works for you. Commented Jul 8, 2013 at 9:07
  • 1
    Check out this website: debuggex.com. Enter any regex you want then have a look to the Some random matches section. You may be surprised with the propositions made when your regex starts to be complicate. Commented Jul 8, 2013 at 9:16
  • What could be generated by .*? Commented Jul 8, 2013 at 9:18
  • Depending on the options everything but line breaks or everything. Commented Jul 8, 2013 at 9:19

4 Answers 4

8

Try using this app Rex can do this :)

http://research.microsoft.com/en-us/projects/rex/

For java it's https://code.google.com/p/xeger/

So there are many regex matches generators :)

And this: https://github.com/moodmosaic/Fare

It's xeger wrapper in c#

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

3 Comments

Rex is cool but due to the license I can not use it.
@schoetbi I found this github.com/moodmosaic/Fare it's wrapper for xeger in C#, try this :)
Fare looks good it includes a Xeger Port. I'll give it a try
2

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.)

1 Comment

I thoght about number 4 since the regex parser of the .net framework is pure managed. But then I found fare that works quite nice. The regex changes and there are several of them and they are also configurable by the user (not the end user though);)
1

as specified in comment, Rex tool will do the trick -

Using Rex to create strings that match your pattern:

run the rex.exe as follows:

rex.exe "your_regex_pattern_here" /k:your_required_examples_num_here

more info regarding this:Rex Guide

Comments

0

Almost certainly not, no.

Regular expressions are generally used, in the context you're looking at, to check that a string matches a given format. If you know what your format should be well enough that you're writing a regular expression for it, there should be no reason why you can't generate your own test data easily enough.

[Edit - it appears there are a few examples around. But this does ignore the fact that, to test that your regex is correct, you must have written test data already. So you should already have your strings.]

6 Comments

I think it should be possible to have a class like the regexparser in .net and instead of checking the rules pick an example of the valid characters for each token in the regex and append it to the example string.
In the situation you describe, you're providing expressions to validate the data against. So surely you know the data already, to have written the expression in the first place?
No the user has not yet entered the string to match. I like to present him valid examples.
But how did you write the regular expression, if you didn't already have valid examples already in mind? Why not present him with those? It's your own app, after all.
I do have it in mind since they are e.g. partnumbers that have to have a certain pattern. The user can pick his own number for his part, but the pattern has to be obeyed
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.