Re.findall() & Re.finditer()

Sort by

recency

|

375 Discussions

|

  • + 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")
    
  • + 0 comments
    import re
    m=list(map(lambda x:x.group(),re.finditer(r"(?<=[QWRTYPSDFGHJKLZXCVBNMqwrtypsdfghjklzxcvbnm])[aeiouAEIOU]{2,}(?=[QWRTYPSDFGHJKLZXCVBNMqwrtypsdfghjklzxcvbnm])",input())))
    if m==[]:
        print(-1)
    else:
        for i in m:
            print(i)
    
  • + 0 comments
    import re
    
    
    pattern = r'(?=[^aeiouAEIOU]([aeiouAEIOU]{2,})[^aeiouAEIOU])'
    
    s = input()
    
    
    matches = re.findall(pattern, s)
    
    if matches:
        for match in matches:
            print(match)
    
    else:
        print(-1)
    
  • + 0 comments

    Here is HackerRank Re.findall() & Re.finditer() in python solution - https://programmingoneonone.com/hackerrank-re-findall-re-finditer-solution-in-python.html

  • + 0 comments

    import re

    S = input()

    vowels = 'AEIOUaeiou' consonants = 'QWRTYPSDFGHJKLZXCVBNMqwrtypsdfghjklzxcvbnm'

    pattern = fr"(?<=[{consonants}])([{vowels}]{{2,}})(?=[{consonants}])"

    matches = re.findall(pattern, S)

    if matches: for x in matches: print(x) else: print(-1)