简体   繁体   English

使用 preg_match PHP 从字符串中查找失败的行

[英]find failed rows from string using preg_match PHP

I need to wright regex using preg_match in PHP to do next:我需要在 PHP 中使用 preg_match 编写正则表达式以执行下一步:

I do have string like this:我确实有这样的字符串:

test1:OK测试1:好的
test2:OK测试2:好的
test3:FAILD测试3:失败
test4:PROGRESS测试4:进度
test5:OK测试5:好的

so I need to find rows which are no OK..所以我需要找到不行的行..
test3:FAILD测试3:失败
test4:PROGRESS测试4:进度

so I think I have to check string between : and \\r\\n (\\n) if != OK, how do I do this using preg_match?所以我想我必须检查:\\r\\n (\\n)之间的字符串,如果 != OK,我该如何使用 preg_match 做到这一点?

Help much appreciated!非常感谢帮助!

Why use regex?为什么要使用正则表达式? String functions should be just fine for this;字符串函数应该适合这个; regexs are more of a last resort.正则表达式更像是最后的手段。

$lines = explode("\n", str_replace("\r\n", "\n", $lines));
$failed = array();

foreach ($lines as $line) {
    if (strpos($line, 'OK') === false) {
        $failed[] = $line;
    }
}

preg_match_all('/test\\d+:(?!OK)/', $allrows, $rowsNotOk)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM