擅长:python、mysql、java
<p>我认为regex不是一个很好的工具。你可以用一个循环。。在</p>
<pre><code>>>> import collections
>>> d = collections.defaultdict(list)
>>> with open('/tmp/spam.txt') as f:
... t = 'initial'
... for line in f:
... if line.startswith('#'):
... t = line.strip()
... else:
... d[t].append(line.strip())
...
>>> for k,v in d.iteritems():
... print k, len(v)
...
#1299 4
#100 3
#1335 6
</code></pre>