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?
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.