regexp-python和sqlite存在问题

regexp-python和sqlite存在问题,python,regex,sqlite,Python,Regex,Sqlite,我尝试在sqlite数据库上使用带有python的正则表达式检查带有模式的字符串。 当我尝试用一个patren使用去搜索字符串时,我遇到了问题 例如: cur.execute("insert into articles(id,subject) values (1,'aaa\"test\"')") cur.execute("select id,subject from articles where id = 1") print (cur.fetchall()) cur.execute("sele

我尝试在sqlite数据库上使用带有python的正则表达式检查带有模式的字符串。 当我尝试用一个patren使用去搜索字符串时,我遇到了问题 例如:

cur.execute("insert into articles(id,subject) values (1,'aaa\"test\"')")
cur.execute("select id,subject from articles where id = 1")
print (cur.fetchall())

cur.execute("select subject from articles where  subject regexp '\"test\"' ")
print (cur.fetchall())
我应该\n在regexp之前以其他方式编译程序不喜欢的方式。。。语法错误

[(1, 'aaa"test"')]
[] <????? should found 
有人知道怎么做吗


我的regexp函数:con.create_functionregexp,2,regexp

您可以使用三重转义或原始字符串

你的工作:

>>> print("select subject from articles where subject regexp '\"test\"' ") select subject from articles where subject regexp '"test"' 或三次逃逸\\\:


使用参数化sql。那么你就不需要自己逃避引用了:

import sqlite3
import re

def regexp(expr, item):
    reg = re.compile(expr)
    return reg.search(item) is not None

conn = sqlite3.connect(':memory:')
conn.create_function("REGEXP", 2, regexp)
cursor = conn.cursor()
cursor.execute('CREATE TABLE foo (bar TEXT)')
cursor.executemany('INSERT INTO foo (bar) VALUES (?)',[('aaa"test"',),('blah',)])
cursor.execute('SELECT bar FROM foo WHERE bar REGEXP ?',['"test"'])
data=cursor.fetchall()
print(data)
屈服

[(u'aaa"test"',)]

另一个参数化查询示例

对于必须向数据库提供自己的正则表达式函数的情况,我想Python sqlite3在默认情况下并不总是设置它

在另一个示例中,自定义正则表达式函数正在为每个匹配编译相同的表达式。这是有办法的。下面的示例在定义正则表达式操作的另一种方法的底部还有一条注释

通过为每个查询创建自定义函数并只编译表达式一次,您可以在每次将表达式用于处理大量数据的查询的每个匹配时编译表达式。self.\u conn下面是数据库连接,curs是其中的游标

    # Form an expression to match nicknames with the last 3 characters
    # varying.
    nick_expr = re.sub(r"[0-9_\-|]{0,3}$", r"[0-9_\-|]{0,3}$", nick)
    nick_expr = re.compile(nick_expr, re.I)

    # Create custom sqlite3 function using the compiled expression.
    self._conn.create_function("NICKEXPR",
                               1,
                               lambda nick: nick_expr.match(nick) != None)

    # Create temporary table from first pass query.
    curs.execute(
        """ CREATE TEMP TABLE temp_table1 AS
           SELECT  DISTINCT *
           FROM    users
           WHERE   NICKEXPR(nick)
               OR  host LIKE ?
               OR  (account<>'' AND account LIKE ?)
               OR  (address<>'' AND address=?)
       """, (host, account, address))

    # Set up the REGEXP operator/function for the sqlite3 database.
    #self._conn.create_function(
    #                       'REGEXP', 2, 
    #                       lambda exp, item : re.find(exp, item) != None)

警告-如果多个线程执行相同的查询,重新定义NICKEXPR可能会影响另一个线程的查询。答案很好!还需要防止regexp中的项为none,例如,如果项为none,则第一行可能为:return False
[(u'aaa"test"',)]
    # Form an expression to match nicknames with the last 3 characters
    # varying.
    nick_expr = re.sub(r"[0-9_\-|]{0,3}$", r"[0-9_\-|]{0,3}$", nick)
    nick_expr = re.compile(nick_expr, re.I)

    # Create custom sqlite3 function using the compiled expression.
    self._conn.create_function("NICKEXPR",
                               1,
                               lambda nick: nick_expr.match(nick) != None)

    # Create temporary table from first pass query.
    curs.execute(
        """ CREATE TEMP TABLE temp_table1 AS
           SELECT  DISTINCT *
           FROM    users
           WHERE   NICKEXPR(nick)
               OR  host LIKE ?
               OR  (account<>'' AND account LIKE ?)
               OR  (address<>'' AND address=?)
       """, (host, account, address))

    # Set up the REGEXP operator/function for the sqlite3 database.
    #self._conn.create_function(
    #                       'REGEXP', 2, 
    #                       lambda exp, item : re.find(exp, item) != None)