I am unable to get my code to count and reset to zero
<p>I am having an issue with creating code that will count when a pattern is not matched and reset back to 0 once that pattern has a match using regular expression. I will list a an example of the file and try to attempt to demonstrate what I am looking to do. No matter how many matches are in the file I receive a -1. I am also not sure how the numbers should be formatted in the text file. </p>

<p>regEx56 "locates all numbers that have 56 in it regardless of location) 256 526 065.
regEx57 "The same as 56 but uses 57 instead"</p>

<pre><code>p3text file.      python count:    56     57
107                                 -1     -1
328                                  -2     -2
156                                   0     -3
725                                   -1     0
</code></pre>

<p>Here is my code:</p>

<pre><code>import re

my_p3text = open("p3text.txt", "r")
p3data = my_p3text.read()


regEx56 = str(re.findall(r'\b[0-4]*(?:5[0-4]?6|6[0-4]?5)[0-4]*\b',p3data))
regEx57 =str(r'\b[0-4]*(?:5[0-4]?7|7[0-4]?5)[0-4]*\b',p3data)



for line in my_p3text:
    match=regEx56.search(line)
    count = 0
    print (match)


else:
    count = -1
print (count)
</code></pre>

<p>First of all:</p>

<pre><code>for line in my_p3text:
    match=regEx56.search(line)
    count = 0
    print (match)


else:
    count = -1
</code></pre>

<p>means that when there are no more lines, it will print -1, which is always. Put an if-else statement inside the for loop.</p>

<p>Also, right now your numbers can only start with 0-4, 5, 6 or 7; with a possible 0-4 between 6-7 and 5 (and vice versa). It also ends in a similar fashion to the way it starts.</p>

<p>If this is what you want, then it's fine, otherwise try...</p>

<pre><code>(\d*((5\d*?[67])|([67]\d*?5))\d*)
</code></pre>

python regex