regex - PowerShell -match 与 -like

标签 regex powershell match

阅读 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/

相关文章:

python - 如何自定义使用 string.punctuation 过滤掉哪些字符?

php - MySQL REGEXP 仅匹配边界词,但不包括 html 标签中的匹配

regex - R 正则表达式仅选择性地替换特定字符串位置的字符

powershell - 使用快照复制 Azure Blob

xml - 用于创建本地用户并将详细信息写入 XML 文件的 Powershell 脚本

powershell - 在网络驱动器上的 .msg 文件中搜索关键字

java - 如何让整个单词使用子字符串?

javascript - 如何用javascript检测一个值是否为空?

MySQL 匹配和喜欢

mysql - 是否可以计算每行与 MATCH AGAINST 匹配的单词数