regexpython-3.xfindallend-of-line

python regex:Consider end of line as an OR condition in a search, similar to characters in character class


Problem: Find all vowels (more than 2) that are sandwiched between two consonants. These vowels can come at beginning or end of line. Example:-

input :-

abaabaabaabaae

expected output :-

['aa','aa','aa','aae']

solution Tried

import re
pattern=re.compile(r'(?:[^aeiouAEIOU])([AEIOUaeiou]{2,})(?=[^AEIOUaeiou])')
pattern.findall("abaabaabaabaae")

This gives output as ['aa','aa','aa'] , it ignores 'aae' for obvious reason as end of line is not part of search criteria. How can I include an anchor - end of line ($) inclusive search such that it($) is an OR condition in the search and not an mandatory end of line.


Solution

  • You can extract matches of the regular expression

    re'(?<=[b-df-hj-np-tv-z])[aeiou]{2,}(?=[b-df-hj-np-tv-z]|$)'
    

    Demo

    For the following string the matches are indicated.

    _abaab_aabaabaaeraaa_babaa%abaa
       ^^     ^^ ^^^             ^^
    

    I found it easiest to explicitly match consonants with the character class

    [b-df-hj-np-tv-z]
    

    Python demo