Findstr-仅返回正则表达式匹配项 [英] Findstr - Return only a regex match
本文介绍了Findstr-仅返回正则表达式匹配项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在文本文件( test.txt
)中有此字符串:
I have this string in a text file (test.txt
):
BLA BLA BLA
BLA BLA
Found 11 errors and 7 warnings
我执行以下命令:
findstr /r "[0-9]+ errors" test.txt
为了得到 11个错误
字符串。
相反,输出为:
Found 11 errors and 7 warnings
有人可以协助吗?
推荐答案
findstr 工具不能仅用于提取匹配项。
The findstr tool cannot be used to extract matches only. It is much easier to use Powershell for this.
这里是一个示例:
$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
请参见 Windows PowerShell:使用正则表达式提取字符串文章,其中介绍了如何使用上述脚本。
See the Windows PowerShell: Extracting Strings Using Regular Expressions article on how to use the script above.
这篇关于Findstr-仅返回正则表达式匹配项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文