我有很多日志文件,想用多行搜索一些模式,但是为了容易找到匹配的字符串,我还是想看到匹配区域的行号。
任何好的建议。(复制代码示例)
string="""
####1
ttteest
####1
ttttteeeestt
####2
ttest
####2
"""
import re
pattern = '.*?####(.*?)####'
matches= re.compile(pattern, re.MULTILINE|re.DOTALL).findall(string)
for item in matches:
print "lineno: ?", "matched: ", item
[更新]行号是实际行号
所以我想要的输出如下:
lineno: 1, 1
ttteest
lineno: 6, 2
ttttteeeestt
您需要的是正则表达式不擅长的典型任务;解析。
您可以逐行读取日志文件,并在该行中搜索用于分隔搜索的字符串。您可以逐行使用regex,但它比常规字符串匹配效率低,除非您正在寻找复杂的模式。
如果你在寻找复杂的匹配,我想看看。在保持行数的同时搜索文件中的每一行以查找
####
在没有regex的情况下更容易。你可以把行号储存在手边,然后再找。
这可以通过以下方式相当有效地完成:
{offset: line_number}
映射存储到最后一个匹配。这样可以避免每次匹配都倒数到文件的开头。
以下函数类似于
re.finditer
如果需要内容,可以将最后一个循环替换为:
请注意,最好避免从
finditer
创建列表,但这意味着我们不知道何时停止存储新行(即使只有模式匹配在文件的开头,它也可能最终存储许多新行)。如果避免存储所有匹配项是很重要的,那么可以根据需要生成一个扫描换行符的迭代器,尽管不确定这会在实践中给您带来多大优势。
相关问题 更多 >
编程相关推荐