I am trying to create a regex expression in Python that deletes the letters 'e' in a word except for in cases where the word is 2 letters long. For example,
Become = Becom, Bee = Be, We = We, Be = Be
How is this possible? I am new to RegEx so any pointers would be appreciated.
CodePudding user response:
You don't have to use regex. Keep untouched the 2 first characters and replace 'e' starting the third characters.
lst = ['Become', 'Bee', 'We', 'Be']
for word in lst:
new_word = word[:2] word[2:].replace('e', '')
print(f'{word:10} -> {new_word}')
Output:
Become -> Becom
Bee -> Be
We -> We
Be -> Be
CodePudding user response:
Without Regex you can solve it like so:
words = ["Become", "Bee", "We", "Be"]
for i, word in enumerate(words):
if len(word) > 2 and word[-1] == "e":
words[i] = word[:-1]
print(words)
And the output will be:
["Becom", "Be", "We", "Be"]
CodePudding user response:
As @Eladtopaz mentioned, you can check
while len(my_word) > 2 and my_word.endswith('e'):
my_word = my_word[:-1]
.endwith because I see the first exapmple that only removes the last 'e', but in the other hand as you wrote in the question, you should check
if len(my_word) > 2 and 'e' in my_word:
my_word = my_word.replace('e', '')
CodePudding user response:
for regex solutions you can do:
(.*[a-zA-Z]){3}= 3 letters (lower or Upper)
e$ = end with e
import re
test=['be','welcome','bee','eNd','enDeDe']
for word in test:
y= re.sub(r"(.*[a-zA-Z]){3}e$",word[:-1],word)
print(f"{word} => {y}")
Result:
be => be
welcome => welcom
bee => bee
eNd => eNd
enDeDe => enDeD
CodePudding user response:
You can use this pattern: (?=[a-zA-Z]{3,})([a-zA-Z] )e$
import re
list_of_words = ["Become", "Bee", "We", "Be"]
pattern = "(?=[a-zA-Z]{3,})([a-zA-Z] )e$"
print([re.sub(pattern, "\\1", x) for x in list_of_words])
Output:
['Becom', 'Be', 'We', 'Be']
CodePudding user response:
You could do it in a list comprehension by dropping the last character of the word using a conditional subscript:
words = ["Become", "Bee", "We", "Be"]
words = [w[:-w[2:].endswith('e') or None] for w in words]
print(words)
['Becom', 'Be', 'We', 'Be']