sanster9292
sanster9292

Reputation: 1325

How do I use regular expression to list words that start with vowels in a sentence

So say, I have a sentence as follows:

sent = "My name is xyz and I got my name from my parents. My email address is [email protected]"

I want to get all the words in this sentence that start with a vowel, so words like is, I, is. This is my regular expression so far and it isn't working.

re.findall('^(aeiou|AEIOU)[\w|\s].',sent)

This is the result I get

['. ', '..', '.s', '@g', '.c']

Any help would be appreciated.

Upvotes: 3

Views: 291

Answers (2)

Selcuk
Selcuk

Reputation: 59297

First of all, your parentheses are not balanced, and you are not checking for word boundaries. Try this:

"\b[(aeiou|AEIOU)].*?\b"

Upvotes: 2

Ajax1234
Ajax1234

Reputation: 71461

You can use re.findall with re.I:

import re
sent = "My name is xyz and I got my name from my parents. My email address is [email protected]"
result = re.findall('(?<=\W)[aeiou]\w+|(?<=\W)[aeiou]', sent, re.I)

Output:

['is', 'and', 'I', 'email', 'address', 'is']

Upvotes: 2

Related Questions