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

问题描述 投票:0回答:3

我在文本文件中有这个字符串(

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

有人可以帮忙吗?

regex cmd findstr
3个回答
11
投票

findstr
始终返回包含匹配项的每个整行,它不能仅返回子字符串。因此,您需要自己进行子字符串提取。无论如何,您的
findstr
命令行中存在一些问题,我想指出:

findstr
的字符串参数实际上定义了多个以空格分隔的搜索字符串,因此一个搜索字符串是
[0-9]+
,另一个是
error
。文本文件中的行
Found 11 errors and 7 warnings
被返回,只是因为单词
error
,数字部分不是匹配的一部分,因为
findstr
不支持
+
字符(前一个字符出现一次或多次)或类),您需要将搜索字符串的该部分更改为
[0-9][0-9]*
才能实现这一点。要将整个字符串视为一个搜索字符串,您需要提供
/C
选项;由于默认为文字搜索模式,因此您还需要显式添加
/R
选项。

findstr /R /C:"[0-9][0-9]* errors" "test.txt"

更改所有这些也会匹配像

x5 errorse
这样的字符串;为了避免这种情况,您可以使用单词边界,例如
\<
(单词开头)和
\>
(单词结尾)。 (或者,您也可以在搜索字符串的两侧添加一个空格,因此
/C:" [0-9][0-9]* errors "
,但是如果搜索字符串出现在适用行的开头或末尾,这可能会导致麻烦。)

因此,对于上述所有内容,更正和改进的命令行如下所示:

findstr /R /C:"\<[0-9][0-9]* errors\>" "test.txt"

这将返回包含匹配项的整行:

Found 11 errors and 7 warnings

如果您只想返回此类行并排除像

2 errors are enough
35 warnings but less than 3 errors
这样的行,您当然可以相应地扩展搜索字符串:

findstr /R /C:"^Found [0-9][0-9]* errors and [0-9][0-9]* warnings$" "test.txt"

无论如何,要提取部分

11 errors
,有多种选择:

  1. a

    for /F
    循环可以解析
    findstr
    的输出并提取某些标记:

    for /F "tokens=2-3 delims= " %%E in ('
        findstr/R /C:"\<[0-9][0-9]* errors\>" "test.txt"
    ') do echo(%%E %%F
    
  2. 还可以使用子字符串替换语法:

    for /F "delims=" %%L in ('
        findstr /R /C:"\<[0-9][0-9]* errors\>" "test.txt"
    ') do set "LINE=%%L"
    set "LINE=%LINE:* =%"
    set "LINE=%LINE: and =" & rem "%"
    echo(%LINE%
    

8
投票

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

请参阅 如何在 PowerShell 中使用正则表达式?有关如何使用上述脚本的文章。


0
投票

使用

Type
(或
Cat
)和
Grep
可以做到这一点。

这将允许随机数量的错误(最多四位数)。

type c:\temp\test.txt | grep -Eo '[0-9]{1,4} errors'

11 个错误

如果错误编号大于四位,请将上面修改为最大的预期数字。

对于精确区分大小写的选项

type c:\temp\test.txt | grep -o "11 errors"

11 个错误

或者这个不区分大小写的选项与

Cat

cat c:\temp\test.txt | grep -o -i "11 ERRORS"

11 个错误

© www.soinside.com 2019 - 2024. All rights reserved.