Python: How to exclude words from regular expressions | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Python: How to exclude words from regular expressions

I want to use regular expressions to remove the letter "t" from the end of each word w in a string ,unless the word is "it" or "at". Below is my code and while it works, I wonder, if there is a more elegant way to code this. import re str = ("wombat", "it", "belt", "at", "adult") words= str. split() for w in words: if w != 'it' and w != 'at': w = re.sub(r't

#x27;, '', w) print(w)

7th Jul 2020, 6:41 PM
Jerry B. Berwick
2 Answers
+ 9
import re words = 'wombat it belt at adult test_word' print(re.sub(r'(\w+)(?<!\b[ai])t\b', r'\1', words)) # womba it bel at adul test_word
7th Jul 2020, 6:48 PM
Russ - avatar
+ 3
A variation on Russ solution: sub(r'(?<!\b[ia])t\b', '', test) to explain: \b means a boundary that encloses words, an invisible marker before or after any word. [ia] mean either i or a character (?<!X)Y is called a negative lookbehind. Just search for this term online to learn more. It causes the expression to match Y only, if X is not found directly before Y. Also, you can solve this without regexp, see in my code a solution with list comprehension.
7th Jul 2020, 7:37 PM
Tibor Santa
Tibor Santa - avatar