Re: Unexpected behavior with regexp model filtering (Sqlite)

46 views
Skip to first unread message

Chris Cogdon

unread,
Dec 18, 2012, 12:44:58 AM12/18/12
to django...@googlegroups.com
Django uses "search" rather than "match" for the _regex option. If you want match, make sure the regex starts with a ^

On Monday, December 17, 2012 6:55:09 PM UTC-8, Beni wrote:
Hi,

I'm getting stuck on figuring out why regex searches are matching any part of a field when I use my model's regexp filter (instead of matching linearly); they're behaving like calls to re.search instead of re.match.

Since I'm using Sqlite, I registered my own function based on Python's re.match. I tried testing the function in a shell by directly connecting to the database to search the field and comparing those results to using the model's regexp filter. The filter returns significantly more items and when I test to see if they are strict matches for the regexp the 'extra' ones fail. 
I'm attaching a transcript of the shell tests.

Any insight or advice would be greatly appreciated.

Beni

Chris Cogdon

unread,
Dec 18, 2012, 12:48:42 AM12/18/12
to django...@googlegroups.com
And for completeness, here's how django implements the regex function when using sqlite3:

def _sqlite_regexp(re_pattern, re_string):
    try:
        return bool(re.search(re_pattern, re_string))
    except:
        return False



Reply all
Reply to author
Forward
0 new messages