2

I am trying to create a method which will look at an Schema object and generate a value for that field which can be accepted. Here is one I am trying to match where the problem came around.

phoneNumber: {
  type: String,
  label: "Phone Number",
  RegExp: /^(\([0-9]{3}\) |[0-9]{3}-)[0-9]{3}-[0-9]{4}$/
}

This will accept a string matching an American style phone numbers, e.g., (111)-111-1111.

For a solution, I thought it'd be possible to recursively build a string and test it against the regex, returning if it matches, but that results in stack overflow (and isn't a great idea to begin with)

characters = frozenset([x for x in string.printable])
def generate_matching_string(re_str, regex):
    " generates a string that matches a given regular expression "
    if re.match(re_str, regex):
        return re_str
    letter = re_str[:-1]
    if characters.index(letter) == len(characters):
        re_str += characters[0]
    else:
        re_str[:-1] = characters[characters.index(letter) + 1]
    return generate_matching_string(re_str, regex)

I imagine the first step would be something similar to a DFA.

Another concern: the result would have to at least be moderately random. That is to say, there should be some variation in results, adding another level of complexity.

How can one generate a string matching a regular expression programmatically? (language agnostic, conceptual is fine).

Community
  • 1
  • 1
corvid
  • 10,733
  • 11
  • 61
  • 130
  • If the pattern is truly regular, build an NFA out of it, convert it to a DFA and randomly traverse it until you reach an ending state. If the pattern is not regular (for instance if it contains lookaheads) - then I wish you (a *lot* of) luck. ;) – Lucas Trzesniewski Apr 02 '15 at 19:06
  • These answers http://stackoverflow.com/questions/22115/using-regex-to-generate-strings-rather-than-match-them include Java libraries for doing this. You could try using them with [GWT](http://www.gwtproject.org/) – joemfb Apr 02 '15 at 19:07
  • I think they _should_ be regular... although finding if it's not regular is a whole different concern entirely – corvid Apr 02 '15 at 19:07
  • You can use already created library for such task called [Xeger](https://code.google.com/p/xeger/) from Google. – MaxZoom Apr 02 '15 at 20:21
  • Only for a specific pattern? Or something that can parse a JSON schema, notice there is a regex, and run a function (testing all known characters) until it has something that passes the regex. – SoEzPz Jul 31 '15 at 19:31
  • Possible duplicate of [Random string that matches a regexp](http://stackoverflow.com/questions/205411/random-string-that-matches-a-regexp) – Sjoerd Apr 13 '17 at 09:21

0 Answers0