Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find all possible substrings beginning with characters from capturing group

Tags:

python

regex

I have for example the string BANANA and want to find all possible substrings beginning with a vowel. The result I need looks like this:

"A", "A", "A", "AN", "AN", "ANA", "ANA", "ANAN", "ANANA"

I tried this: re.findall(r"([AIEOU]+\w*)", "BANANA") but it only finds "ANANA" which seems to be the longest match. How can I find all the other possible substrings?

like image 478
roOt Avatar asked Feb 17 '16 12:02

roOt


1 Answers

s="BANANA"
vowels = 'AIEOU'
sorted(s[i:j] for i, x in enumerate(s) for j in range(i + 1, len(s) + 1) if x in vowels)
like image 77
Magnus Lyckå Avatar answered Nov 05 '22 06:11

Magnus Lyckå