<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def regex(r):
 """ Create a PEG function to match a regular expression. """

    if isinstance(r, stringtypes):
        p = re.compile(r)
    else:
        p = r
    msg = 'Expected to match: {}'.format(p.pattern)
    def match_regex(s, grm=None, pos=0):
        m = p.match(s)
        if m is not None:
            start, end = m.span()
            data = m.groupdict() if p.groupindex else m.group()
            return PegreResult(s[m.end():], data, (pos+start, pos+end))
        raise PegreError(msg, pos)
    return match_regex