ipconfig | Select-String "IPv4 Address"
返回如下内容:
IPv4 Address. . . . . . . . . . . : 192.168.1.50
IPv4 Address. . . . . . . . . . . : 172.20.112.1
IPv4 Address. . . . . . . . . . . : 192.168.208.1
说我想用-replace用空字符串替换“ IPv4地址……………………:” 。我该怎么办?这是我尝试过的:
ipconfig | Select-String "IPv4 Address" -replace "IPv4 Address. . .",""
这给了我以下错误:
Select-String : A parameter cannot be found that matches parameter name 'replace'.
At line:1 char:41
+ ipconfig | Select-String "IPv4 Address" -replace "IPv4 Address. . .", ...
+ ~~~~~~~~
+ CategoryInfo : InvalidArgument: (:) [Select-String], ParameterBindingException
+ FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.SelectStringCommand
有任何想法吗?
仅使用Select-String
,您可以使用捕获组来提取IP地址:
ipconfig | Select-String "IPv4 Address.*:\s*(.+)" | ForEach-Object { $_.Matches.Groups[1].Value }
.*:
匹配所有 :
\s*
与:
IP地址之间的任何(零个或多个)空格匹配(.+)
匹配IP地址(又一个字符)并将其捕获到组1中$_.Matches.Groups[1].Value
输出捕获组1的值,即IP地址该链接很好地概述了将RegEx与PowerShell一起使用的多种可能性。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句