acronym.py

1
2
3
4
5
6
7
8
9
10
import re

ABBREV_MATCH = re.compile(r'''
    (?:\b(\w)|          # Any letter after word boundary
    ([A-Z])[a-z]+)      # Capital letter followed by at least one lower case
''', re.VERBOSE)

def abbreviate(phrase):
    matches = ABBREV_MATCH.findall(phrase)
    return ''.join((m[0] or m[1]).upper() for m in matches)

@de2Zotjes thinks this looks great

Comments

Most regex solutions are hardly readable, but you managed to be very concise and really clear. Beautiful.

de2Zotjes commented 31 March 2016 at 19:43 UTC

Thanks! re.VERBOSE is a beautiful thing :)

helenst commented 1 April 2016 at 15:07 UTC

You're not logged in right now. Please login via GitHub to comment