使用PHP过滤纯文本文件中的特定行

时间:2016-04-18 14:24:15

标签: php regex

您好我在从文本文件中过滤某些行时遇到问题。当我运行程序时,会生成一个纯文本文件,列出与验证文件相关的错误消息。但是我需要忽略一些生成的错误,主要是错误描述中搜索路径中导入或找不到的错误,如下所示:

static-state.y:8: error: module "inet-types" not found in search path
mpls-static-state.y:11: error: module "y-types" not found in search path
mpls-static-state.y:11: warning: imported module y-types not used
mpls-static-state.y:15: error: module "context-state" not found in search path
mpls-static-state.y:15: warning: imported module context-state not used
mpls-static-state.y:19: error: module "contexts" not found in search path

我将需要其他错误消息,因此我无法完全清空文件,同时我也在阅读文本文件并在网页上显示这些错误。我从文件中读到这样的内容:

if(file_exists("stderr.txt")){
    $fh = fopen("stderr.txt", 'r');
    $errorOutput = fread($fh, 25000);
}

然后我使用$ errorOutput来存储文本文件中的内容。什么是过滤我不想要的错误行的最佳方法?试图创建一个正则表达式,但没有任何运气让它工作可以有人帮忙吗?

1 个答案:

答案 0 :(得分:1)

这可以通过 主播 向前看 来实现:

^(?:.(?!(?:not found)))+$
# ^  - match the beginning of the line
# (?:.) - match any character except a newline
# (?! - negative lookahead
# $ - end of the line

请参阅 a demo on regex101.com (请注意multiline修饰符!)。

相关问题