like
运算符
-like
接受 wildcard 匹配。
*
可以匹配任意数量的字符。
?
匹配任意字符之一。
[]
包含要匹配一次的字符范围。
# Matches 127.0.<anything>
Get-NetIPAddress | where IPAddress -like '127.0.*'
# Matches 127.0.0.<one character>
Get-NetIPAddress | where IPAddress -like '127.0.0.?'
# Matches 127.0.0.<one number between 0 and 9>
Get-NetIPAddress | where IPAddress -like '127.0.0.[0-9]'
match
运算符
-match
使用正则表达式。
这增加了更多的灵活性和复杂性。
.
可以匹配这里的任何字符,所以字面点应该用反斜杠转义。
# Matches 127.0.0.<one number between 0 and 9>
Get-NetIPAddress | where IPAddress -match '127\.0\.0\.[0-9]'
# Matches 127.<any number>.<any number>.<any number>
Get-NetIPAddress | where IPAddress -match '127\.[0-9]+\.[0-9]+\.[0-9]+'
# Matches 127.0.<one number between 0 and 9>.<two digit number>
Get-NetIPAddress | where IPAddress -match '127\.0\.[0-9]\.[0-9]{2}'
0 条评论