所以现在我在找一份文件。我得到了一个value
变量,它是一个相当长的字符串,有新行等等。然后,我用关于芬德尔(regex,value)查找regex。Regex相当简单-类似于“abcde.*”。在
现在,我不仅要捕获regex所拥有的任何内容,而且还要捕获上下文(与grep
的-C
标志完全相同)。在
所以,假设我将value
转储到文件中并对其运行grep,那么我要做的就是grep -C N 'abc de .*' valueinfile
如何在Python中实现相同的功能?我需要答案来使用Unicode regex/text。在
Tags:
As recommended通过Ignacio Vazquez-Abrams,使用a deque来存储最后的n行。一旦有许多行出现,popleft为每个新行添加。当正则表达式找到匹配项时,返回堆栈中以前的n行,然后迭代n更多行并同时返回这些行。在
这使您不必在任何行上迭代两次(干),并且只在内存中存储最小的数据。您还提到了对Unicode的需要,因此处理文件编码并向RegEx搜索添加Unicode标志非常重要。另外,另一个答案使用重新匹配()而不是搜索()因此可能产生意想不到的后果。在
下面是一个例子。这个例子只对文件中的每一行迭代一次,这意味着同样包含命中的上下文行将不再被查看。这可能是也可能不是理想的行为,但可以很容易地进行调整,以突出显示或以其他方式在上下文中标记前一次命中的行。在
我的方法是将文本块拆分为行列表。接下来,遍历每一行,看看是否有匹配项。在匹配的情况下,收集上下文行(发生在当前行之前和之后的行)并返回它。这是我的代码:
输出:
^{pr2}$相关问题 更多 >
编程相关推荐