在辅音之间找出每两个(不重叠的)元音

2024-09-26 17:38:18 发布

您现在位置:Python中文网/ 问答频道 /正文

Task You are given a string . It consists of alphanumeric characters, spaces and symbols(+,-). Your task is to find all the substrings of the origina string that contain two or more vowels. Also, these substrings must lie in between consonants and should contain vowels only.

Input Format: a single line of input containing string .

Output Format: print the matched substrings in their order of occurrence on separate lines. If no match is found, print -1.

Sample Input:rabcdeefgyYhFjkIoomnpOeorteeeeet

Sample Output:

ee
Ioo
Oeo
eeeee


上面的挑战来自https://www.hackerrank.com/challenges/re-findall-re-finditer

以下代码通过了所有测试用例:

import re

sol = re.findall(r"[^aiueo]([aiueoAIUEO]{2,})(?=[^aiueo])", input())

if sol:
    for s in sol:
        print(s)
else:
    print(-1)

下面的不是

^{pr2}$

它们之间唯一的区别是正则表达式的最后一位。我不明白为什么第二个代码失败了。我认为?=是无用的,因为通过分组[aiueoAIUEO]{2,}我已经把它排除在捕获之外了,但显然我错了,我不知道为什么。在

有什么帮助吗?在


Tags: andoftheinreformatinputstring
1条回答
网友
1楼 · 发布于 2024-09-26 17:38:18

先行法允许结束一个元音序列的辅音启动下一个序列,而非前瞻方法要求这些序列之间至少有两个辅音(一个结束一个序列,另一个开始下一个序列,因为两者都匹配)。在

看到了吗

import re
print(re.findall(r'[^aiueo]([aiueoAIUEO]{2,})(?=[^aiueo])', 'moomoom'))
print(re.findall(r'[^aiueo]([aiueoAIUEO]{2,})[^aiueo]', 'moomoom'))

它将输出

^{pr2}$

https://ideone.com/2Wn1TS

有点挑剔,这两种尝试对于您的问题描述都是不正确的。它们允许大写元音、空格和符号作为分隔符。您可能希望使用[b-df-hj-np-tv-z]而不是[^aeiou],并使用flags=re.I

相关问题 更多 >

    热门问题