On Friday, December 29, 2017 at 12:50:24 PM UTC-8, Dave S wrote:
>
>
>
> On Friday, December 29, 2017 at 6:16:04 AM UTC-8, Anthony wrote:
>>
>> You could do something like:
>>
>> query = (db.mytable.myfield.like('mytext,%') |    # First item in list
>>          db.mytable.myfield.like('%,mytext,%') |  # Neither first nor 
>> last item
>>          db.mytable.myfield.like('%,mytext') |    # Last item in list
>>          db.mytable.myfield.like('mytext'))       # Single item (no list)
>>
>> If you make sure to start and end every list (even single item lists) 
>> with a separater (e.g., ",a,b,c," instead of "a,b,c"), then you can 
>> simplify it to just:
>>
>> db.mytable.myfield.like('%,mytext,%')
>>
>> The above is how the DAL handles list:-type fields.
>>
>> Another alternative is a regexp search:
>>
>> db.mytable.myfield.regexp(r'\bmytext\b')
>>
>> The above will search for "mytext" between any word boundaries -- if the 
>> list items themselves might include word boundaries (e.g., spaces), then 
>> you'll need a more precise regular expression. 
>>
>> Be careful about allowing arbitrary user input for a regexp query (see 
>> caution here: 
>> https://www.postgresql.org/docs/current/static/functions-matching.html).
>>
>> Anthony
>>
>
> Thank you.  I can be picky about what characters are allowed in the 
> request, and the intended use won't be harmed by the restriction, so I may 
> go with the regex.  (And a python regex to check the user input.)
>
>
> /dps
>
>>
>>

It's probably worth a couple notes about the regex syntax.  It depends, of 
course, on what support the DB engine has for regular expressions.  So far, 
I've only looked at this for Sqlite3 -- which doesn't have its own regex 
support.  Instead, the application provides a function (a callback, I'd 
call it) for that purpose.  If you're Googling for more information, you'll 
probably see that the usual package is PERLRE, and you might use the 
references for that to try to figure out how to compose your more esoteric 
r.e.s.   But in the web2py environment,, it's the DAL adapter for Sqlite3 
that provides the function, and it just uses the Python standard library 
routines.  Very similar, but not quite identical, Look here for details
<URL:https://docs.python.org/2/library/re.html>

The two packages agree on how to match a string that doesn't contain a 
pattern xyz:

v = "xyz"
db.mytable.tags.regexp(r"(?!" + v + r")")

Lots of people here probably had this figured out already, but if this post 
saves an hour for some small child whose assignment is due tomorrow ....

/dps

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to