gpt4 book ai didi

regex - Findstr-仅返回正则表达式匹配项

转载 作者:行者123 更新时间:2023-12-04 14:09:31 24 4
gpt4 key购买 nike

我在文本文件(test.txt)中有以下字符串:

BLA BLA BLA
BLA BLA
Found 11 errors and 7 warnings

我执行以下命令:
findstr /r "[0-9]+ errors" test.txt

为了只获得 11 errors字符串。

相反,输出为:
Found 11 errors and 7 warnings

有人可以协助吗?

最佳答案

findstr工具不能仅用于提取匹配项。为此,使用Powershell更加容易。

这是一个例子:

$input_path = 'c:\ps\in.txt'
$output_file = 'c:\ps\out.txt'
$regex = '[0-9]+ errors'
select-string -Path $input_path -Pattern $regex -AllMatches | % { $_.Matches } | % { $_.Value } > $output_file

有关如何使用以上脚本的信息,请参见 the Windows PowerShell: Extracting Strings Using Regular Expressions article

关于regex - Findstr-仅返回正则表达式匹配项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40780784/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com