'Escape user input to place in a regex
Python\'s re.escape escapes unicode characters in a way which postgres
fails on, u\'\u03bb\' to u\'\\u03bb\'. This function will correctly escape
them for postgres, u\'\u03bb\' to u\'\u03bb\'.'

def _pg_re_escape(self, pattern):

    s = list(pattern)
    for (i, c) in enumerate(s):
        if (c not in self._alphanum):
            if (ord(c) >= 128):
                s[i] = '\\u{:0>4x}'.format(ord(c))
            else:
                s[i] = ('\\' + c)
    return ''.join(s)
