gpt4 book ai didi

python - 正则表达式模式查找最长的元音序列

转载 作者:行者123 更新时间:2023-12-01 02:33:12 27 4
gpt4 key购买 nike

我的下面的代码用于定位元音的第一个匹配,在本例中为 'ua' 但如何将正则表达式配置为 1) 在第一个结尾之前搜索整个字符串找到并 2) 一旦完成步骤 1,确定所有模式中哪一个显示元音的最长并发性?

import re
s = "quality"
matches = re.search(r"[aeiou]+", s)
matchlist = matches.group().split(', ')
print(len(matchlist))

最佳答案

抓取所有匹配项,然后找到最长的匹配项:

import re
s = "quality"
matches = re.findall(r"[aeiou]+", s) # Finds all chunks of vowel letters
print(max(matches, key=len)) # Gets the longest item in the list
# => ua

请参阅Python demo .

关于python - 正则表达式模式查找最长的元音序列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46552440/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com