+ 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 Réponses
+ 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
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. https://docs.python.org/3.8/library/re.html Also, you can solve this without regexp, see in my code a solution with list comprehension. https://code.sololearn.com/cU5Lf6FqRpXW/?ref=app
7th Jul 2020, 7:37 PM
Tibor Santa
Tibor Santa - avatar