Reputation: 1197
I am having a regular expression to match a particular pattern. Say, a pattern that will match all three letter words. But i want it to not match words like 'and','got' etc... What would be the best way to do it ,in Python.
My pattern is
r'\b\w{3}\b'
I tried
r'(\b\w{3}\b)(?!and)'
but fails.
Upvotes: 0
Views: 97
Reputation: 22457
Regexes match left to right, and lookaheads are no exception. Your expression will match three letters that are not followed by and
(which is impossible because of the \b
, by the way).
Move the lookahead before the \w
to make it work:
r'(\b(?!and)\w{3}\b)'
You can add more words there --
r'(\b(?!and|got|may)\w{3}\b)'
but for more non-matches it may be more effective to match all three letter words and use code to strip the result of them.
Upvotes: 4