Composable Regexp in Python
Question: Often, I would like to build up complex regexps from simpler ones. The only
way I'm currently aware of of doing this is through string operations, e.g.:

    
    
    Year = r'[12]\d{3}'
    Month = r'Jan|Feb|Mar'
    Day = r'\d{2}'
    HourMins = r'\d{2}:\d{2}'
    
    Date = r'%s %s, %s, %s' % (Month, Day, Year, HourMins)
    DateR = re.compile(Date)
    

Is anybody aware of a different method or a more systematic approach (maybe a
module) in Python to have composable regexps? I'd rather compile each regexp
individually (e.g. for using individual compile options), but then there
doesn't seem to be a way of composing them anymore!?


Answer: You can use Python's formatting syntax for this:

    
    
    types = {
        "year":           r'[12]\d{3}',
        "month":        r'(Jan|Feb|Mar)',
        "day":            r'\d{2}',
        "hourmins":    r'\d{2}:\d{2}',
    }
    import re
    Date = r'%(month)s %(day)s, %(year)s, %(hourmins)s' % types
    DateR = re.compile(Date)
    

(Note the added grouping around Jan|Feb|Mar.)

