Parsing with regex
<p>I'm trying to count the number of lines contained by a file that looks like this:</p>

<pre><code>-StartACheck
---Lines--
-EndACheck
-StartBCheck
---Lines--
-EndBCheck
</code></pre>

<p>with this:</p>

<pre><code>count=0
z={}
for line in file:
      s=re.search(r'\-+Start([A-Za-z0-9]+)Check',line)
      if s:
           e=s.group(1)
           for line in file:
               z.setdefault(e,[]).append(count)
               q=re.search(r'\-+End',line)
               if q:
                   count=0
                   break

for a,b in z.items():
    print(a,len(b))
</code></pre>

<p>I want to basically store the number of lines present inside ACheck , BCheck etc in a dictionary but I keep getting the wrong output</p>

<p>Something like this</p>

<pre><code>A,15
B,9
</code></pre>

<p>etc</p>

<p>If the file isn't too big (say, smaller than 1GB or so), I'd just read the whole thing and call <code>re.findall()</code>:</p>

<pre><code>import re
result = {
    name: lines.count('\n')
    for name, lines in
    re.findall(r'^-Start([A-Za-z0-9]+)Check$(.*)^-End\1Check$',
               open('x.in').read(),
               re.DOTALL + re.MULTILINE)
    }
print (result)
</code></pre>

python regex