We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Re.findall() & Re.finditer()
Re.findall() & Re.finditer()
Sort by
recency
|
375 Discussions
|
Please Login in order to post a comment
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 :
Here is HackerRank Re.findall() & Re.finditer() in python solution - https://programmingoneonone.com/hackerrank-re-findall-re-finditer-solution-in-python.html
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)