Re.findall() & Re.finditer()

  • + 0 comments

    I hope it could help someone for this exercise :

    With regex exercises, we have to search in the documentation what we can do, because the exercises instructions obviously doesn't give everything.

    For this exercise, we have to use the "lookaround" operators. ( ?<= and ?= ) It allows us to specify which character we want before and after a group.

    It's easy to remember how to write these operator because "?<=" is like a left-arrow, so "before" the group that we search.

    Here my code, where even if the regex is long, it's only because I have specified all consonants and vowels. But I think there is a way to put them into a variable above, to have a more lisible regex :

    # Enter your code here. Read input from STDIN. Print output to STDOUT
    import re
    
    S = input().strip()
    
    result = re.findall(r"(?<=[BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz])([AEIOUaeiou]{2,})(?=[BCDFGHJKLMNPQRSTVWXYZbcdfghjklmnpqrstvwxyz])", S)
    
    if result:
        print('\n'.join(result))
    else:
        print("-1")