26

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.

Community
  • 1
  • 1
wishi
  • 7,188
  • 17
  • 64
  • 103

3 Answers3

41

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.

bjmc
  • 2,970
  • 2
  • 32
  • 46
11

The exrex module does this: https://github.com/asciimoo/exrex.

Community
  • 1
  • 1
asciimoo
  • 631
  • 5
  • 9
  • +1 Works like a charm! It even install with `easy_install` :) – Morten Jensen Mar 20 '13 at 20:02
  • 7
    Alas, it's GPL; you can't use it in commercial software without contaminating it. – JDonner Apr 14 '13 at 22:13
  • 1
    https://bitbucket.org/leapfrogdevelopment/rstr/ has a `xeger` method and is MIT – fjsj Aug 19 '16 at 21:14
  • 2
    To clarify JDonner's comment for anyone finding this: it's AGPL; you can't even use it in server-side commercial software without releasing the source code to the entire executable that uses this library. – btown Nov 02 '17 at 16:01
  • @JDonner, btown - you're saying that like it's a bad thing. – user234461 Apr 27 '18 at 15:59
  • 2
    I guess it's not necessarily a bad thing, but it's very important that people understand the requirements of the license before using the library! – Dobes Vandermeer Sep 03 '18 at 17:31
0

For some regular expressions, the list of possible strings can be infinite. For example:

a*

includes

a
aa
aaa

etc. Thus, there is no way to generate all strings for a given regex.

chrisaycock
  • 36,470
  • 14
  • 88
  • 125
  • 3
    More to the point, any function you write to generate an arbitrary string could get stuck in an infinite loop at a* – fredley Jan 07 '11 at 16:13
  • 4
    asciimoo points to `exrex` which is implemented using generators. That way you can easily control the output and the size of it. – Morten Jensen Mar 20 '13 at 20:30
  • 3
    Not really. The requirement is to match a regex. "a" works, no need to generate all the possible scenarios. – Cyril N. Mar 17 '21 at 14:21