based on Wilfred Springer's solution together with
http://www.brics.dk/~amoeller/automaton/ i build another generator.
It do not use recursion. It take as input the patter/regularExpression minimum String length and maximum String length. The result is an accepted String between min and max length. It also allow some of the XML "short hand character classes".
I use this for an XML Sample Generator that build valid String for facets.
public static final String generate(final String pattern, final int minLength, final int maxLength) {
final String regex = pattern
.replace("\\d", "[0-9]") // Used d=Digit
.replace("\\w", "[A-Za-z0-9_]") // Used d=Word
.replace("\\s", "[ \t\r\n]"); // Used s="White"Space
final Automaton automaton = new RegExp(regex).toAutomaton();
final Random random = new Random(System.nanoTime());
final List<String> validLength = new LinkedList<>();
int len = 0;
final StringBuilder builder = new StringBuilder();
State state = automaton.getInitialState();
Transition[] transitions;
while(len <= maxLength && (transitions = state.getSortedTransitionArray(true)).length != 0) {
final int option = random.nextInt(transitions.length);
if (state.isAccept() && len >= minLength && len <= maxLength) validLength.add(builder.toString());
final Transition t = transitions[option]; // random transition
builder.append((char) (t.getMin()+random.nextInt(t.getMax()-t.getMin()+1))); len ++;
state = t.getDest();
}
if(validLength.size() == 0) throw new IllegalArgumentException(automaton.toString()+" , "+minLength+" , "+maxLength);
return validLength.get(random.nextInt(validLength.size()));
}