1

I want to search with r.match in rethinkdb using user input - whole user input should be treated as search pattern. I'm creating regex based on user input like this:

pattern = u'.*(?i){0}.*'.format(user_input)

But if user inputs '[' or ']' or any other special character I got errors from rethink like this:

Error in regexp .*(?i)\u0141[.* (portion [.*): missing ]: [.* in:

Is there any way to escape regex for rethinkdb? I have tried to use python re.escape but this fail when combining unicode with errors like this one:

Error in regexp .*(?i)\\u0141.* (portion \\u0141): invalid escape sequence: \u0141

Any suggestions?

sop3k
  • 101
  • 2
  • 8
  • I don't know if this is still relevant for you since the question was asked 4 years ago but in case someone else stumbles across it I had the same issue and found a workaround for it: https://stackoverflow.com/a/55435019/8952681 Hope it answers your question and solves this mystery. – Bogdan Mar 30 '19 at 19:37

1 Answers1

0

Use the re.escape() function for this:

re escape

escape(string)

Return string with all non-alphanumerics backslashed; this is useful if you want to match an arbitrary literal string that may have regular expression metacharacters in it.

vks
  • 67,027
  • 10
  • 91
  • 124
  • I was trying to do so, but when using unicode I got errors like this one: Error in regexp .*(?i)\\u0141.* (portion \\u0141): invalid escape sequence: \u0141 – sop3k May 30 '15 at 19:12