我有一个生成并包含各种文件引用的报表文件。我使用String和正则表达式来匹配某些类型的文件,并对它们执行后续处理。
当有0 (0)、1 (1)或多个(2+)匹配时,我面临的困境是试图一致地识别匹配的数量。以下是我尝试过的:
(select-string -path $outputfilePath -pattern $regex -allmatches).matches.count如果有0匹配,则返回"null“,如果有一个匹配,则返回"1”,如果多个匹配,则返回"null“。
(select-string -path $outputfilePath -pattern $regex -allmatches).count如果有0或1匹配,则返回"null“,如果有多个匹配,则返回匹配数。
我是Powershell的新手,但我正试图找到一种一致的方法来测试匹配的数量,而不管是否有0、1或多个匹配。
发布于 2013-09-06 23:53:06
可以使用@(...)将结果始终放置在带有Count的集合中。
(Select-String ...) # may return $null, one match, or a collection of matches
(Select-String ...).Count # only succeeds for two or more matches
@(Select-String ...) # always returns a collection of matches
@(Select-String ...).Count # always succeedshttps://stackoverflow.com/questions/18623419
复制相似问题