Skip to content Skip to sidebar Skip to footer

Python Unicode Escape For Rethinkdb Match (regex) Query

I am trying to perform a rethinkdb match query with an escaped unicode user provided search param: import re from rethinkdb import RethinkDB r = RethinkDB() search_value = u'\u05

Solution 1:

From what it looks like RethinkDB rejects escaped unicode characters so I wrote a simple workaround with a custom escape without implementing my own logic of replacing characters (in fear that I must miss one and create a security issue).

import re

defno_unicode_escape(u):
    escaped_list = []

    for i in u:
        iford(i) < 128:
            escaped_list.append(re.escape(i))
        else:
            escaped_list.append(i)

    rv = "".join(escaped_list)
    return rv

or a one-liner:

import re

defno_unicode_escape(u):
    return"".join(re.escape(i) iford(i) < 128else i for i in u)

Which yields the required result of escaping "dangerous" characters and works with RethinkDB as I wanted.

Post a Comment for "Python Unicode Escape For Rethinkdb Match (regex) Query"