simplere / README.rst

A simplified interface to Python's regular expression (re) string search. As a bonus, also provides compatible way to access Unix glob searches.


Python regular expressions are powerful, but the language's lack of an en passant assignment requires a preparatory motion and then a test, like this:

import re

match =, some_string)
if match:

With simplere, you can do it in fewer steps:

from simplere import Re

if some_string in Re(pattern):
    print Re._[1]

There are two things to note here: First, this turns the sense of the matching around, asking "is the given string in the set of items this pattern describes?" To be fancy, the Re pattern is an intensionally defined set (namely "all strings matching the pattern"). This order often makes excellent sense whey you have a clear intent for the test. For example, "is the given string within the set of all legitimate commands?"

Second, the in test had the side effect of setting the underscore name _ to the result. Python doesn't support en passant assignment, so you can't both test and collect results in the same motion, even though that's sometimes exactly appropriate.

If you prefer the more traditional re calls, you can still use them with the convenient en passant style.:

if Re(pattern).search(some_string):
    print Re._[1]

Re works even better with named pattern components, such as:

person = 'John Smith 48'
if person in Re(r'(?P<name>[\w\s]*)\s+(?P<age>\d+)'):
    print, "is", Re._.age, "years old"
    print "don't understand '{}'".format(person)

It's possible also to loop over the results:

for found in Re('pattern (\w+)').finditer('pattern is as pattern does'):
    print found[1]

Or collect them all in one fell swoop:

found = Re('pattern (\w+)').findall('pattern is as pattern does')

Pretty much all of the methods and properties one can access from the standard re module are available.

Re objects are memoized for efficiency, so they so they're only compiled once, regardless of how many times they're mentioned in the program.

Bonus: Globs

Regular expressions are wonderfuly powerful, but sometimes the simpler Unix glob is works just fine. As a bonus, simplere also provides simple glob access.:

if 'globtastic' in Glob('glob*'):
    print "Yes! It is!"
    raise ValueError('YES IT IS')


  • simplere is part of a larger (as yet unpublished) effort to add intensional sets to Python.


pip install -U simplere

(You may need to prefix this with "sudo " to authorize installation.)