python - 查找字符串中的最后一个元音

标签 python string

我似乎找不到正确的方法来搜索字符串中的最后一个元音,并在最后一个元音之后存储任何唯一的辅音。到目前为止我已经这样设置了。

word = input('Input a word: ')
wordlow = word.lower()
VOWELS = 'aeiou'
last_vowel_index = 0

for i, ch in enumerate(wordlow):
    if ch == VOWELS:
        last_vowel_index += i

print(wordlow[last_vowel_index + 1:])

最佳答案

我喜欢COLDSPEED's方法,但为了完整起见,我会建议一个基于正则表达式的解决方案:

import re
s = 'sjdhgdfgukgdk'
re.search(r'([^AEIOUaeiou]*)$', s).group(1)
# 'kgdk'

# '[^AEIOUaeiou]'  matches a non-vowel (^ being the negation)
# 'X*'  matches 0 or more X
# '$' matches the end of the string
# () marks a group, group(1) returns the first such group

请参阅docs on python regular expression syntax 。唯一性部分还需要进一步处理;)

关于python - 查找字符串中的最后一个元音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46406958/

相关文章:

python - 如何用插值拟合 ODE 系统?

string - 从 Prolog 中的字符串中删除空格

javascript - 我怎样才能使这个 "percentage-only"Regex 工作?

python - Pytest-html:根据标记自定义报告标题

python - API 调用失败 - 错误 400

c++ - 如何从仅包含字母的 vector 字符串中删除?我的代码没有删除我需要删除的每个字符串

java - 如何使用输入大小修饰符打印长类型值?

string - 用于比较具有不同长度字符串的 2 个文件的 Bash 脚本

python - Python 中复杂列表理解语句的操作顺序和内部创建资源的数量

python - 创建一个在两列之间具有 bool 条件的表