PowerShell-匹配与-like [英] PowerShell -match vs -like

查看:142
本文介绍了PowerShell-匹配与-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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆