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

-- 
You received this message because you are subscribed to the Google Groups 
"Django users" group.
To view this discussion on the web visit 
https://groups.google.com/d/msg/django-users/-/YSL83-vSqhAJ.
To post to this group, send email to django-users@googlegroups.com.
To unsubscribe from this group, send email to 
django-users+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/django-users?hl=en.

Bensiins-MacBook-Pro:myproject benicorp$ python manage.py shell
Python 2.7.3 (v2.7.3:70274d53c1dd, Apr  9 2012, 20:52:43) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> import sqlite3, re
>>> from settings import DATABASES
>>> def regexp(pattern, item):
...     reg = re.compile(pattern)
...     return reg.match(item) is not None
... 
>>> loc = DATABASES['default']['NAME']
>>> conn = sqlite3.connect(loc)
>>> conn.create_function("REGEXP", 2, regexp)
>>> c=conn.cursor()
>>> c.execute("select * from myapp_word where arpabet REGEXP '; M.*'")
<sqlite3.Cursor object at 0x101cb55e0>
>>> res=c.fetchall()
>>> len(res)
8639
>>> test=[r for r in res if r[2][2]!="M"]
>>> len(test)
0
>>> conn.close()
>>> from myapp.models import Word
>>> res2 = Word.objects.filter(arpabet__regex='; M.*')
>>> res2=list(res2)
>>> len(res2)
18333
>>> test2 =[r for r in res2 if r.arpabet[2]!="M"]
>>> len(test2)
9694
>>> 18333-8639
9694

Reply via email to