python - Parenthesis in MySQL Regex queries with SQLAlchemy -


i have python application searches database using sqlalchemy so:

query = raw_input('search: ') db.session.query(posts).filter(posts.name.op('regexp')(r'[[:<:]]{}'.format(query))).all() 

this works fine mysql database characters have found searches not work.

  • searches include ( without ) following somewhere after return error
  • searches contain foreign characters Б, э, д or ц returns error

is there solution support kinds of search queries including special , foreign characters? thanks.

searches include ( without ) following somewhere after return error

this happening since ( , ) have special meaning in regular expressions. expression not valid, if, example, there opening parenthesis without closing one:

>>> re.compile(r"test (") traceback (most recent call last):   file "<console>", line 1, in <module>   ... error: unbalanced parenthesis 

you need escape query:

import re  query = re.escape(query) 

Comments

Popular posts from this blog

java - pagination of xlsx file to XSSFworkbook using apache POI -

Unlimited choices in BASH case statement -

apache - How do I stop my index.php being run twice for every user -