Home > Mobile >  How to match words in a string which contain at least one vowel (a,i,o,u,e) using regex python
How to match words in a string which contain at least one vowel (a,i,o,u,e) using regex python

Time:10-13

It seems straightforward task, but I could not solve. I am fresh at using re module

string1 = 'www Cristian www Bale www' --- Here is my test string.

pattern1 = '([aıoueəiöü])' --- Pattern


import re

string1 = 'www Cristian Bale www'

pattern1 = '([aıoueəiöü])'

result = re.findall(pattern1, string1)

print(result)

It results just vowels which words have: ['i', 'i', 'a', 'a', 'e']

My expected output is actually, these words: "Cristian Bale". How can I return these?

CodePudding user response:

You can use

import re
string1 = 'www Cristian Bale www'
pattern1 = r'\b[^\Waıoueəiöü]*[aıoueəiöü]\w*'
result = re.findall(pattern1, string1)
print(" ".join(result))
# => Cristian Bale

See the Python demo. Details:

  • \b - a word boundary
  • [^\Waıoueəiöü]* - any word char except the aıoueəiöü chars
  • [aıoueəiöü] - a letter from the set
  • \w* - zero or more word chars.

The " ".join(result) part creates a single string out of the extracted string list.

CodePudding user response:

import re
string1 = 'www Cristian www Bale www'
vowels = re.findall(r'\w*[aeiou]\w*', string1)
print(' '.join(vowels))

OUTPUT

Cristian Bale
  • Related