views:

86

answers:

2

Dear reader,

In my application the user can enter his own regex pattern into a text box so he can force a certain input for another user/text box. Is it possible for the user to see an example of a string that would match the regex he has entered? For example if he was to enter: ^[A-Z]{2}$, it would generate a string like "XX" to show the user he can only enter two capital letters.

If there's no easy way to accomplish this (and I assume there isn't), how difficult would it be to build? Or does something like this already exist?

+2  A: 

Check out Xeger. It looks like it can do what you want. It's in Java though.

Here is an example from the test suite:

   @Test
    public void shouldGenerateTextCorrectly() {
        String regex = "[ab]{4,6}c";
        Xeger generator = new Xeger(regex);
        for (int i = 0; i < 100; i++) {
            String text = generator.generate();
            assertTrue(text.matches(regex));
        }
    }
Martin Wickman
That's exactly what I'm looking for.. but for C#. At least I have something I can use as reference.
Kevin van Zanten
Xeger is just a thin wrapper around http://www.brics.dk/~amoeller/automaton/ which is DFA/NFA implementation of some regexp operations. Maybe there is something like that available for C# somewhere?
Martin Wickman