Unable to parse through sqlite query using regular expressions
<p>I am trying to figure out a way to iterate through a database and match all rows that have 02 in them using regular expressions. When a match is made the count should reset to 0 and when there is not a match the count should accumulate by negative 1. The code works when I use a list. </p>

<pre><code>import sqlite3
import re

conn = sqlite3.connect('p34.db')

c = conn.cursor()



r = re.compile(r'\b[5-9]*(?:0[5-9]?2|2[5-9]?0)[5-9]*\b')
q = "SELECT Number FROM 'Pick 3'"



c.execute(q)
rez = c.fetchall()
count = 0 


for i in rez:
    if i == r:
        count = 0
    else:
        count = count -1

print(count)
conn.close()
print (rez)
</code></pre>

<p>You probably want to use <a href="https://docs.python.org/3/library/re.html#re.search" rel="nofollow"><code>r.search()</code></a> to execute a regular expression search:</p>

<pre><code>for i in rez:
    if r.search(i[0]):
        count = 0
    else:
        count -= 1
</code></pre>

<p>Note the use of <code>i[0]</code> to get the first value from each tuple returned by the query.</p>

<p>Using <code>i == r</code> simply <em>compares</em> the tuple <code>i</code> (as returned by the query) to the compiled regular expression patter <code>r</code> and will never be true. It does not execute any of the search methods of the given pattern.</p>

python regex sqlite3