繁体   English   中英

正则表达式模式以找到最长的元音序列

[英]Regex pattern to find the longest vowel sequence

我的下面代码可用于定位元音的第一个匹配项,在这种情况下为'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演示

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM