pythonregexpython-regex

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


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?


Solution

  • 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:

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