0

I'm testing MongoAlchemy for a project and I've to search user by name.

I'm trying to make a regex but query result is always empty.

I tried two methods :

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

And :

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

Even if I use a very general regex like /.*/, the result is always empty.

Thank you.

amiceli
  • 437
  • 5
  • 11
  • 29
  • 3
    Maybe because you should be using `a` instead of `/a/`? You are using Python patterns, and regex delimiters are never part of Python regexps. – Wiktor Stribiżew Apr 26 '16 at 13:56
  • Yeah. Strip those slashes which in other contexts (Perl, sed or whatever) may mean "search". Here you just have to give the regexp itself. Calling it with `filter` states that a search is required. – Alfe Apr 26 '16 at 13:58

1 Answers1

1

In python regular expressions are not defined using /regexp/, this is javascript syntax.

The proper way to initialize regular expressions would be:

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

So you should use:

users = User.query.filter({"name": re.compile(r".*", re.IGNORECASE)}).all()
tobspr
  • 8,200
  • 5
  • 33
  • 46