简体   繁体   English

如何逃避rethinkdb正则表达式模式进行r.match

[英]How to escape rethinkdb regex pattern for r.match

I want to search with r.match in rethinkdb using user input - whole user input should be treated as search pattern. 我想使用用户输入在rethinkdb中使用r.match搜索-整个用户输入应被视为搜索模式。 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)\Ł[.* (portion [.* ): missing ]: [.* in: 正则表达式中的错误.*(?i)\Ł[.*[.*部分]:丢失]:[。* in:

Is there any way to escape regex for rethinkdb? 有什么办法可以逃脱正则表达式的rethinkdb吗? I have tried to use python re.escape but this fail when combining unicode with errors like this one: 我尝试使用python re.escape,但是在将unicode与此类错误结合时,此操作将失败:

Error in regexp .*(?i)\\\Ł.* (portion \\\Ł ): invalid escape sequence: \Ł 正则表达式中的错误.*(?i)\\\Ł.* (部分\\\Ł ):无效的转义序列:\\ u0141

Any suggestions? 有什么建议么?

Use the re.escape() function for this: re.escape()请使用re.escape()函数:

re escape re逃脱

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. 如果您想匹配其中可能包含正则表达式元字符的任意文字字符串,这将很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM