<p>为什么不使用多行正则表达式-然后脚本变成:</p>
<pre><code>import re
# read all the text of the file, and clean it up
with open('counts.txt', 'rt') as f:
alltext = '\n'.join(line.strip() for line in f)
# find all occurrences of the script line followed by the launch line
cont = re.findall('^script(\d)\nlaunch(\d+)\.VBS\n(?mi)',alltext)
# accumulate the counts of each launch number for each script number
# into nested dictionaries
scriptcounts = {}
for scriptnum,launchnum in cont:
# if we haven't seen this scriptnumber before, create the dictionary for it
if scriptnum not in scriptcounts:
scriptcounts[scriptnum]={}
# if we haven't seen this launchnumber with this scriptnumber before,
# initialize count to 0
if launchnum not in scriptcounts[scriptnum]:
scriptcounts[scriptnum][launchnum] = 0
# incremement the count for this combination of script and launch number
scriptcounts[scriptnum][launchnum] += 1
# produce the output in order of increasing scriptnum/launchnum
for scriptnum in sorted(scriptcounts.keys()):
for launchnum in sorted(scriptcounts[scriptnum].keys()):
print "script%s\nlaunch%s.VBS\n# count %d\n"%(scriptnum,launchnum,scriptcounts[scriptnum][launchnum])
</code></pre>
<p>输出(以您要求的格式)是,例如:</p>
^{pr2}$
<p>在关于芬德尔()返回所有匹配项的列表-每个匹配项都是模式中()部分的列表,除了(?)?mi)这是一个指令,用于告诉正则表达式匹配器跨行结束\n并匹配不区分大小写的匹配。regex模式(如fragment'script(\d)'将脚本/启动后的数字拉入匹配-这可以很容易地包括'script'(script\d)',类似的是'(launch\d+\.VBS'),只有打印需要修改才能处理这种变化。在</p>
<p>高温
巴尼</p>