Regex Powershell-如何在文件中查找模式
我在一个文件中有一个VisualStudio项目生成的输出。我想读取该文件,找到模式并删除文件中除模式本身之外的所有内容 要查找的模式看起来像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
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"] } }