阅读 official docs 很明显,PowerShell -match
运算符比 -like
更强大(由于正则表达式)。其次,根据这篇文章 https://www.pluralsight.com/blog/software-development/powershell-operators-like-match ,它似乎快了大约 10 倍。
是否有特定情况下我应该更喜欢 -like
而不是 -match
?如果没有,我为什么要使用 -like
?是否因为历史原因而存在?
最佳答案
从 Differences Between -Like
and -Match
看到这个解释
In a nutshell, if you are thinking, 'I am probably going to need a wildcard to find this item', then start with
-Like
. However, if you are pretty sure of most of the letters in the word that you are looking for, then you are better off experimenting with-Match
.Here is a more technical distinction:
-Match
is a regular expression, whereas-Like
is just a wildcard comparison, a subset of-Match
.
因此,当您不确定内部可以包含哪些字符类(即数字、字母、标点符号等)时,当您只想匹配任何字符时,您应该使用
-Like
及其通配符。如果您知道开头必须有一个数字,后跟 1+ 个冒号序列,然后是字母数字字符,直到字符串末尾,请使用
-Match
及其强大的 regular expressions 。
关于regex - PowerShell -match 与 -like,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36295016/