1

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?

2 Answers2

1

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.

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
0
import re
string1 = 'www Cristian www Bale www'
vowels = re.findall(r'\w*[aeiou]\w*', string1)
print(' '.join(vowels))

OUTPUT

Cristian Bale
Mohamed Darwesh
  • 659
  • 4
  • 17