[Python] Use of OR (|) does not return results equal to the individual regexes

Discussão em 'Python' iniciado por Stack, Julho 19, 2025.

  1. Stack

    Stack Membro Participativo

    Here's the problem statement:


    Return the original text where in that original text:

    1. after each consonant a random vowel has been inserted
    2. after each vowel, the vowel has been replicated twice
    ​

    And what I've tried:

    text = "hieeelalaooo" #original string is hello
    pat1 = "(?=[^aeiouy][aeiouy])\\w" #const then random vowel
    ans1=re.findall(pat1,text)
    #ans1 = ['h', 'l', 'l']
    pat2 = "([aeiouy])\\1\\1" #vowel replicated twice
    ans2=re.findall(pat2,text)
    #ans2 = ['e', 'o']
    ans = re.findall(pat1 + "|" + pat2, text)
    #ans = ['', 'e', '', '', 'o']


    I was hoping that ans would be 'hello'. I'm sure there are a numerous ways to meet the goal, but I'm interested in why this doesn't work.

    Continue reading...

Compartilhe esta Página