Regular expressions reverser for Python
Switch branches/tags
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.
LICENSE Add LICENSE file Dec 6, 2013
README.rst Update README, fixing PyPI badges Aug 19, 2015
requirements-test.txt Sort out tox and Travis config files Dec 6, 2013
tox.ini Fix for Python 3.6 Sep 16, 2017



unmatcher tries to solve the following problem:

Given a regular expression, find any string that matches the expression.

Why? Mostly just because. But one possible application is to generate test data for string processing functions.


PyPI package version License Build Status

Most typical elements of regexes are supported:

  • multipliers: *, +
  • capture groups: |, ( ) (including backreferences)
  • character classes (\d|\w|\s etc.) and character sets ([])


unmatcher module exposes a single reverse function. It takes a regular expression - either in text or compiled form - and returns a random string that matches it:

>>> import unmatcher
>>> print unmatcher.reverse(r'\d')

Additional arguments can be provided, specifying predefined values for capture groups inside the expression. Use positional arguments for numbered groups ('\1', etc.):

>>> import unmatcher
>>> print unmatcher.reverse(r'<(\w+)>.*</\1>', 'h1')

and keyword arguments for named groups:

>>> import unmatcher
>>> print unmatcher.reverse('(?P<foo>\w+)__(?P=foo)', foo='bar')

Note that a predefined value is not validated against actual subexpression for the capture group.