MongoAlchemy正则表达式什么都不返回

时间:2016-04-26 13:48:25

标签: python regex mongodb mongoalchemy

我正在测试MongoAlchemy的项目,并且我按名称搜索用户。

我尝试制作正则表达式但查询结果始终为空。

我尝试了两种方法:

import re
users = User.query.filter({"name":re.compile("/a/", re.IGNORECASE)}).all()

并且:

users = User.query.filter(User.name.regex('/a/', ignore_case=True)).all()

即使我使用像 /.*/ 这样的非常通用的正则表达式,结果也总是空的。

谢谢。

1 个答案:

答案 0 :(得分:1)

在python regular expressions中没有使用/regexp/定义,这是javascript语法。

初始化正则表达式的正确方法是:

re.compile(r".*", re.IGNORECASE)

所以你应该使用:

users = User.query.filter({"name": re.compile(r".*", re.IGNORECASE)}).all()