How can I find words in a string that have the last letter of a vowel and the penultimate consonant using regular expressions?

    1 answer 1

    Try:

    import re text = 'Как найти в строке слова, у которых последняя буква гласная, а предпоследняя согласная при помощи регулярных выражений?' vowels = 'аоиеёэыуюя' consonants = 'бвгджзйклмнпрстфхцчшщьъ' pattern = r'\b[{0}{1}]+[{0}][{1}]\b'.format(consonants, vowels) words = re.findall(pattern, text, flags=re.IGNORECASE) print(words) # ['найти', 'строке', 'слова', 'буква', 'при', 'помощи']