Generate a string that matches a regular expression
posted 10 years ago
What I want to do sounds so simple, but it looks it is not available in Java. Normally regular expressions are used to check an input string against the expression. But I need it the other way around. I want to generate a string value which is valid for a regular expression. So for instance if the regular expression is [A-Z][0-9] a good string value will be A0. I have no control about the regular expressions I will get as input. In fact they can be present in xsd schemas to restrict the valid values in XML messages. It is OK to have always the same generated string for a specific regular expression, in other words the value does not need to be random. This means [A-Z] may always return A0. The only purpose is to get a value that is valid for the regex.
So the signature of this method I need, should be something like this:
I know it is possible to write such a method by myself. But I think it is a lot of work. Anyone who knows of a public library where this method is present?