PowerShell-匹配与-like [英] PowerShell -match vs -like
问题描述
阅读官方文档显然,PowerShell -match
运算符是比-like
更强大(由于正则表达式).其次,根据本文 https://www.pluralsight.com/blog/software-development/powershell-operators-like-match .
Reading official docs it's obvious that PowerShell -match
operator is more powerful than -like
(due to regular expressions). Secondly, it seems ~10 times faster according to this article https://www.pluralsight.com/blog/software-development/powershell-operators-like-match.
在某些情况下,我应该首选-like
而不是-match
吗?如果没有,为什么我应该全部使用-like
?是否由于历史原因而存在?
Are there specific cases when I should prefer -like
instead of -match
? If there not, why at all should I use -like
? Does it exist because of historical reasons?
推荐答案
请参见 -Like
和-Match
简而言之,如果您在想,"我可能需要通配符才能找到该项目",然后从
-Like
开始.但是,如果您可以确定要查找的单词中的大多数字母,那么最好尝试使用-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
.
这里有一个技术上的区别:-Match
是正则表达式,而-Like
只是通配符比较,是-Match
的子集.
Here is a more technical distinction: -Match
is a regular expression, whereas -Like
is just a wildcard comparison, a subset of -Match
.
因此,每当不确定什么字符类别(例如,数字,字母,标点符号等)时,只要想与任何字符匹配,就应该使用-Like
及其通配符.
So, whenever you are not sure what character classes, i.e. digits, letters, punctuation, etc., can there be inside, when you just want to match any character, you should be using -Like
with its wildcards.
如果您知道开头必须有一个数字,然后是1+个冒号序列,然后是字母数字字符,直到字符串的末尾,请使用-Match
及其强大的
When you know there must be a digit at the start followed with 1+ sequences of a colon followed with alphanumeric characters up to the end of the string, use -Match
with its powerful regular expressions.
这篇关于PowerShell-匹配与-like的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!