Reputation: 305
So I want to write a regex that matches with a word that is one character less than the word. So for example:
wordList = ['inherit', 'inherent']
for word in wordList:
if re.match('^inhe....', word):
print(word)
And in theory, it would print both inherit and inherent, but I can only get it to print inherent. So how can I match with a word one letter short without just erasing one of the dots (.)
Upvotes: 0
Views: 238
Reputation: 3740
(Edited)
For matching only inherent, you could use .{4}
:
re.match('^inhe.{4}', word)
Or ....$
:
re.match('^inhe....$')
Upvotes: 3
Reputation: 2296
A regex may not be the best tool here, if you just want to know if word Y starts with the first N-1 letters of word X, do this:
if Y.startswith( X[:-1] ):
# Do whatever you were trying to do.
X[:-1] gets all but the last character of X (or the empty string if X is the empty string).
Y.startswith( 'blah' ) returns true if Y starts with 'blah'.
Upvotes: 0