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

时间:2021-08-06 02:22:49

Hi I'm having a problem with filtering certain lines out of a text file. When I run a program a plain text file is generated listing error messages related to validating a file. However I need to ignore some of the errors generated, mainly ones with imported or not found in search path in the error description like the ones below:

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

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

There will be other error messages that I will need so I can't empty the file completely, also I'm reading the text file and displaying these errors on a webpage. I read from the file like this:

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

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

Then I use the $errorOutput to store what is in the text file. What would be the best way to filter the error lines I don't want out? Was trying to create a regex but not having any luck getting it working can anyone help?

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

1 个解决方案

#1


1  

This can be achieved with anchors and lookaheads:

这可以通过锚点和前瞻来实现:

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

See a demo on regex101.com (mind the multiline modifier!).

请参阅regex101.com上的演示(请注意多线修改器!)。

#1


1  

This can be achieved with anchors and lookaheads:

这可以通过锚点和前瞻来实现:

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

See a demo on regex101.com (mind the multiline modifier!).

请参阅regex101.com上的演示(请注意多线修改器!)。