Search code examples
pythonregexstringbuilder

Generate a String that matches a RegEx in Python


Possible Duplicate:
Reversing a regular expression in python

I think I ran into a problem that sounds easier than it is... I'm not too sure. I want to define a regular expression, and I want to build a number of strings matching it.

Is there any module I can import that has got this functionality? Preferably not a brute-force approach using re.search or re.match. There must be a more elegant way to do that.


Solution

  • I've been working on a little helper library for generating random strings with Python

    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.