Skip to content

Instantly share code, notes, and snippets.

@burnto
Created November 21, 2012 22:09
Show Gist options
  • Save burnto/4128186 to your computer and use it in GitHub Desktop.
Save burnto/4128186 to your computer and use it in GitHub Desktop.
Get all words matching a pattern
import re
def match(pattern):
"""
>>> match(' x ')
['axe', 'oxy']
>>> match('c m n l')
['communal', 'criminal', 'crumenal', 'culminal']
"""
r = re.compile('^' + re.sub(' ', '.', pattern) + '$', re.IGNORECASE)
with open('/usr/share/dict/words') as f:
return [w for w in [w.strip() for w in f.readlines()] if r.match(w)]
if __name__ == "__main__":
import doctest
doctest.testmod()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment