def encode_delimiter_re(delimiter):
    """
    encode a delimiter regexp, replacing any ^ and $ with newlines
    """
    if delimiter[0] == '^' or delimiter[-1] == '$':
        delimiter = list(delimiter)
        if delimiter[0] == '^':
            delimiter[0] = '\n'
        if delimiter[-1] == '$':
            delimiter[-1] = '\n'
        delimiter = ''.join(delimiter)
    return '{}'.format(delimiter).encode('UTF-8')
