我正在学习PowerShell,希望在变量中匹配一个字符串。考虑一下这个例子:
$string = ipconfig
Select-String -InputObject $string -Pattern '127.0.0.1'返回整个字符串。不只是'127.0.0.1'.,所以我试着:
Select-String -InputObject $string -SimpleMatch '127.0.0.1' -AllMatches它还返回整个字符串。我做错了什么?我只想看比赛,而不是其他台词。
发布于 2017-09-14 11:26:16
发布于 2017-09-14 11:17:52
Select-String返回一个.Matches属性,它是匹配项的集合。它的.Value属性是匹配的值:
$string = ipconfig
(Select-String -InputObject $string -Pattern '127.0.0.1').Matches.Value此示例将返回所有看似IP地址的值:
(Select-String -InputObject $string -Pattern '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}' -AllMatches).Matches.Value请注意,如果您正在匹配一个确切的模式(例如,没有通配符/regex),那么您可以只使用-Quiet,它根据模式是否匹配返回true/false:
$MyString = '127.0.0.1'
If (Select-String -InputObject $string -Pattern $MyString -Quiet) { $MyString }然后
https://stackoverflow.com/questions/46217532
复制相似问题