简体   繁体   English

__iregex = searchterm在Django中不起作用

[英]__iregex=searchterm not working in django

I have this query: 我有这个查询:

q = MyModel.objects.filter(title__iregex=r"\y{0}\y".format(searchterm))

but once I give c++ as searchterm, i am getting this error: 但是一旦我将c++用作搜索词,就会出现此错误:

Error:  invalid regular expression: quantifier operand invalid

how can I make signs like +, /, -, % passable thru regex? 我如何通过正则表达式使诸如+, /, -, %通过的符号?

Escape them: c\\+\\+ . 转义它们: c\\+\\+ Maybe escape them twice, if sth is evaluating one escaping: c\\\\+\\\\+ . 如果sth正在评估一个转义符,则可能逃脱两次: c\\\\+\\\\+

If you want to do that programmatically, use re.escape() : 如果要以编程方式执行此操作,请使用re.escape()

import re

print re.escape('c++')

This will print 这将打印

c\+\+

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

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