删除元音,除非它是单词的开头

sm1*_*m15 3 python

我试图删除字符串中元音的出现,除非它们是单词的开头.所以例如输入"The boy is about to win" 应该输出.这Th by is abt t wn是我到目前为止所拥有的.任何帮助,将不胜感激!

def short(s):
vowels = ('a', 'e', 'i', 'o', 'u')
noVowel= s
toLower = s.lower()
for i in toLower.split():
    if i[0] not in vowels:
        noVowel = noVowel.replace(i, '')        
return noVowel
Run Code Online (Sandbox Code Playgroud)

FMc*_*FMc 6

一种方法是使用正则表达式替换不在单词边界之前的元音.此外,如果您的代码应该处理具有各种类型标点符号的任意文本,您可能需要考虑一些更有趣的测试用例.

import re
s = "The boy is about to win (or draw). Give him a trophy to boost his self-esteem."
rgx = re.compile(r'\B[aeiou]', re.IGNORECASE)
print rgx.sub('', s)  # Th by is abt t wn (or drw). Gv hm a trphy t bst hs slf-estm.
Run Code Online (Sandbox Code Playgroud)