I'm working with a Python 2.7.2 script to find lists of words inside of a text file that I'm using as a master word list.
I am calling the script in a terminal window, inputting any number of regular expressions, and then running the script.
So, if I pass in the two regular expressions "^.....$" and ".*z" it will print every five letter word that contains at least one "z".
What I am trying to do is add another regular expression to EXCLUDE a character from the strings. I would like to print out all words that have five letters, a "z", but -not- a "y".
Here is the code:
import re
import sys
def read_file_to_set(filename):
words = None
with open(filename) as f:
words = [word.lower() for word in f.readlines()]
return set(words)
def matches_all(word, regexes):
for regex in regexes:
if not regex.search(word):
return False
return True
if len(sys.argv) < 3:
print "Needs a source dictionary and a series of regular expressions"
else:
source = read_file_to_set(sys.argv[1])
regexes = [re.compile(arg, re.IGNORECASE)
for arg in sys.argv[2:]]
for word in sorted(source):
if matches_all(word.rstrip(), regexes):
print word,
What modifiers can I put onto the regular expressions that I pass into the program to allow for me to exclude certain characters from the strings it prints?
If that isn't possible, what needs to be implemented in the code?