Reversing a regular expression in Python

python, regex

Solution

Somebody else had a similar (duplicate?) question here, and I'd like to offer a little helper library for generating random strings with Python that I've been working on.

It includes a method, `xeger()` that allows you to create a string from a regex:

>>> import rstr
>>> rstr.xeger(r'[A-Z]\d[A-Z] \d[A-Z]\d')
u'M5R 2W4'

Right now, it works with most basic regular expressions, but I'm sure it could be improved.

Problem

I want to reverse a regular expression. I.e. given a regular expression, I want to produce any string that will match that regex. I know how to do this from a theoretical computer science background using a finite state machine, but I just want to know if someone has already written a library to do this. :) I'm using Python, so I'd like a Python library. To reiterate, I only want one string that will match the regex. Things like "." or ".*" would make an infinite amount of strings match the regex, but I don't care about all options. I'm willing for this library to only work on a certain subset of regex.

Original source

Related problems