Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Regex Powershell-如何在文件中查找模式_Regex_Powershell - Fatal编程技术网

Regex Powershell-如何在文件中查找模式

Regex Powershell-如何在文件中查找模式,regex,powershell,Regex,Powershell,我在一个文件中有一个VisualStudio项目生成的输出。我想读取该文件,找到模式并删除文件中除模式本身之外的所有内容 要查找的模式看起来像23失败 文本文件如下所示: Microsoft Visual Studio Copyright (C) Microsoft Corp. All rights reserved. 1>------ Build started: Project: Test, Configuration: Debug ------ 1> Creating li

我在一个文件中有一个VisualStudio项目生成的输出。我想读取该文件,找到模式并删除文件中除模式本身之外的所有内容

要查找的模式看起来像
23失败

文本文件如下所示:

Microsoft Visual Studio Copyright (C) Microsoft Corp. All rights reserved. 1>------ Build started: Project: Test, Configuration: Debug ------ 1> Creating library S:\Test\Test.lib and object S:\Test\Test.exp 1>Test.vcxproj -> S:\Test\Debug\Test.dll ========== Build: 1 succeeded, 23 failed, 12 up-to-date, 0 skipped ==========
您可以使用名称组提取所需内容,而不是进行替换:

Get-Content $Path | % { if ($_ -match '(?<FailCount>\d+ failed)') { $Matches["FailCount"] } }
Get Content$Path |%{if($_-match'(?\d+failed){$Matches[“FailCount”]}
/\s(\d.失败)/g我认为您的查询保留了整行内容。
Get-Content $Path | % { if ($_ -match '(?<FailCount>\d+ failed)') { $Matches["FailCount"] } }