word compression function using regular expressions in python
Question: Can someone tell me why it seems I am not getting the right results using this
regular expression in this python code? I would have thought, for example,
that the initial vowel in the word "about" should not disappear. Thanks.

    
    
    >>> sentence = "But the third reason Americans should care about Europe is more important    even than the risk of a renewed financial crisis."
    >>> regexp = r'^[AEIOUaeiou]+|[AEIOUaeiou]+$|[^AEIOUaeiou]'
    >>> def compress(word):
    ...     pieces = re.findall(regexp, word)
    ...     return ''.join(pieces)
    >>> compress(sentence)
    'Bt th thrd rsn mrcns shld cr bt rp s mr mprtnt vn thn th rsk f  rnwd fnncl crss.'
    


Answer: ^ and $ anchor to the ends of the entire string, so you're not anchoring to
the beginning and end of each word, but to the beginning and end of the entire
sentence. When the sentence is only the word "about," it works as you expect.
I think you want to anchor to word boundaries (\b) instead.

<http://www.regular-expressions.info/wordboundaries.html>

This might do what you want it to:

    
    
    regexp = r'\b[AEIOUaeiou]+|[AEIOUaeiou]+\b|[^AEIOUaeiou]'
    

