当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] PowerShell 匹配 |匹配运算符在 PowerShell 中如何工作?

作者:精品下载站 日期:2024-12-14 04:57:25 浏览:15 分类:玩电脑

PowerShell 匹配 |匹配运算符在 PowerShell 中如何工作?


[玩转系统] PowerShell 匹配 |匹配运算符在 PowerShell 中如何工作?

PowerShell 匹配的定义

PowerShell 匹配运算符(Like、NotLike、Match、NotMatch)使用提供的模式或通配符检查输入字符串或关键字是否与特定关键字匹配。使用的模式和通配符取决于所使用的运算符。 -Like 和 -NotLike 运算符使用通配符, -Match 和 -NotMatch 运算符使用正则表达式。

语法:

以下语法用于 PowerShell 匹配运算符。

<string[]> -like    <wildcard-expression>
<string[]> -notlike <wildcard-expression>
<string[]> -match    <regular-expression>
<string[]> -notmatch <regular-expression>

匹配运算符在 PowerShell 中如何工作?

A. -Like 和 -NotLike 运算符。

PowerShell -Like-NotLike 运算符与 -Eq-Ne 运算符类似,但它们使用通配符来匹配输入字符串。以下是用于匹配模式的通配符。

通配符:

*

匹配零个或多个字符

?

匹配该位置的一个字符

[]

匹配一系列字符

[]

匹配特定字符

通配符 (*) 匹配字符串中的零个或多个字符,如果输入字符串是世俗字符串,则它给出布尔输出,如果它是字符串数组,则匹配的字符串将是输出,如下所示。

"PowerShell" -like "Power*"

输出:

[玩转系统] PowerShell 匹配 |匹配运算符在 PowerShell 中如何工作?

在上面的示例中,通配符 (*) 与其前面的字符串匹配(不区分大小写)。

"PowerShell" -like "Power"

输出:

[玩转系统] PowerShell 匹配 |匹配运算符在 PowerShell 中如何工作?

如上所示,如果没有通配符,-like 运算符将无法工作。当我们在字符串之前使用通配符时,它会检查它是否与字符串的结尾部分匹配,如下所示。

"PowerShell" -like "*Shell"

输出:

[玩转系统] PowerShell 匹配 |匹配运算符在 PowerShell 中如何工作?

当在两个通配符 (*) 之间添加关键字时,它会检查该关键字是否是输入字符串的一部分。例如,

"PowerShell" -like "*rsh*"

输出:

[玩转系统] PowerShell 匹配 |匹配运算符在 PowerShell 中如何工作?

-NotLike 运算符将给出相反的输出。

"PowerShell" -Notlike "*rsh*"

输出:

[玩转系统] PowerShell 匹配 |匹配运算符在 PowerShell 中如何工作?

上面的示例适用于单个输入字符串。如果有多个字符串作为输入值,则它将返回输入字符串中的匹配值。

"PowerShell","Azure","Insight" -like "*Sh*"

输出:

[玩转系统] PowerShell 匹配 |匹配运算符在 PowerShell 中如何工作?

- 不像例子。

"PowerShell","Azure","Insight" -notlike "*Sh*"

输出:

[玩转系统] PowerShell 匹配 |匹配运算符在 PowerShell 中如何工作?

通配符([] - 方括号)用于匹配特定字符或字符范围。

当通配符 [] 与破折号 ('-') 一起使用时,它匹配字符串中的字符范围。如果找到,则返回 True 值,否则返回 false 值。

"PowerShell" -like "p[m-p]wershell"

输出:

[玩转系统] PowerShell 匹配 |匹配运算符在 PowerShell 中如何工作?

在上面的例子中,字符‘o’在[m-p]范围内,所以上面的输出为true。

类似地,如果我们使用方括号内的字符集,它将与其中的输入字符匹配。例如,

"PowerShell" -like "[pq]owershell"

输出:

[玩转系统] PowerShell 匹配 |匹配运算符在 PowerShell 中如何工作?

在上面的示例中,第一个字符在集合 [pq] 中匹配,因此输出为 true。以下输出将为 false,因为字符“p”不属于 [abc] 集。

"PowerShell" -like "[abc]owershell"

[玩转系统] PowerShell 匹配 |匹配运算符在 PowerShell 中如何工作?

B. -Match 和 -NotMatch 运算符。

-Match-NotMatch 运算符与正则表达式一起使用。

  • 字符文字:

当我们使用部分字符进行匹配时,如果字符存在,-Match 运算符返回 True 输出,否则返回 False。例如,

"IndPC002" -match "IND"

输出:

[玩转系统] PowerShell 匹配 |匹配运算符在 PowerShell 中如何工作?

  • 角色组
"IndPC002" -match "[ijk]nd[opq]C002"

输出:

[玩转系统] PowerShell 匹配 |匹配运算符在 PowerShell 中如何工作?

在上面的示例中,将字符组与特定位置处的输入字符进行比较。如果匹配,则返回输出,否则返回 false。

  • 字符范围
"PS" -match "[A-Z][A-Z]"

输出:

[玩转系统] PowerShell 匹配 |匹配运算符在 PowerShell 中如何工作?

"PS" -cmatch "[A-Z][0-9]"

输出:

[玩转系统] PowerShell 匹配 |匹配运算符在 PowerShell 中如何工作?

  • 十进制和非十进制数字。

正则表达式字符 \d 匹配十进制数字,而 \D 匹配任何非十进制数字。

2021 -match "\d"

输出:

[玩转系统] PowerShell 匹配 |匹配运算符在 PowerShell 中如何工作?

"2021c" -match "\D"

输出:

[玩转系统] PowerShell 匹配 |匹配运算符在 PowerShell 中如何工作?

上面的输出为 true,因为它包含非数字字符。

  • 单词字符。

要检查输入字符串是否包含单词,我们需要使用 \w, ,相反, \W 表示非单词字符类。

"PowerShell" -match "\w"

输出:

[玩转系统] PowerShell 匹配 |匹配运算符在 PowerShell 中如何工作?

  • 空格和通配符。

空格与‘\s’字符匹配,非空格与\S字符匹配。

"a c" -match "[ab]\s[a-z]"

输出:

[玩转系统] PowerShell 匹配 |匹配运算符在 PowerShell 中如何工作?

对于通配符匹配,使用句点 (.) 符号。它匹配除换行符 (\n) 之外的任何字符。

"a c" -match "..."
"a c" -match "...."

输出:

[玩转系统] PowerShell 匹配 |匹配运算符在 PowerShell 中如何工作?

[玩转系统] PowerShell 匹配 |匹配运算符在 PowerShell 中如何工作?

  • 量词
+

一次或多次

*

零次或多次

?

零次或一次

{n,m}

最小 N,最大 M

示例:

  • 星号(*) 匹配前面的字符零次或多次,即使字符不存在,输出也将为true。
"Hello   PowerShell" -match "\w*\s*PowerShell"

输出:

[玩转系统] PowerShell 匹配 |匹配运算符在 PowerShell 中如何工作?

  • 加号 (+) 符号与前一个字符匹配一次或多次。
"IndNZPC-001" -match "[A-Z]+-\d+"

输出:

[玩转系统] PowerShell 匹配 |匹配运算符在 PowerShell 中如何工作?

  • 问号 (?) 与前一个字符匹配零次或一次。在下面的示例中,即使破折号 (-) 不存在,输出也将为 true。
"IndNZPC001" -match "[A-Z]+-?\d+"

输出:

[玩转系统] PowerShell 匹配 |匹配运算符在 PowerShell 中如何工作?

  • {n,m} 限定符可以以不同的方式使用,如下表所示。
{n}

精确匹配 N 次

{n,}

匹配至少N次

{n,m}

最小 N 次和最大 M 次

例子 :

'1800-1800-123' -match '\d{4}-\d{4}-\d{3}'

输出:

[玩转系统] PowerShell 匹配 |匹配运算符在 PowerShell 中如何工作?

  • 使用锚点:

PowerShell 主要使用两个锚点。 脱字符号 (^)美元 ($)

插入符 (^) 匹配字符串的开头。例如,以下命令将显示所有以“d”开头的进程。

Get-Process | where{$_.Name -match "^d"}

输出:

[玩转系统] PowerShell 匹配 |匹配运算符在 PowerShell 中如何工作?

美元 ($) 用于在字符串末尾进行匹配。例如,以下命令将检索所有以 Host 结尾的进程。

Get-Process | where{$_.Name -match "host$"}

输出:

[玩转系统] PowerShell 匹配 |匹配运算符在 PowerShell 中如何工作?

您还可以组合使用脱字符 (^)美元 ($) 符号。

示例

示例#1:像运营商一样过滤特定服务。

Get-Service | where{$_.Name -like "sp*"}

输出:

[玩转系统] PowerShell 匹配 |匹配运算符在 PowerShell 中如何工作?

上面的示例将检索所有以 SP 开头的服务名称。

Get-Process | where{$_.Name -notlike "*win*"}

[玩转系统] PowerShell 匹配 |匹配运算符在 PowerShell 中如何工作?

上面的示例将检索进程名称中不包含“win”的所有进程。

示例#2:用于名称验证的匹配运算符

"IndPC-002" -match "\w-\d"
"IndPC002" -match "\w-\d"

输出:

[玩转系统] PowerShell 匹配 |匹配运算符在 PowerShell 中如何工作?

上面的示例验证了服务器名称模式。

示例#3:PowerShell 函数 ValidatePattern

我们可以将上面的示例与 Function ValidatePattern 属性一起使用,如下所示。

function ServerValidation{
param(
[ValidatePattern("\w-\d")]$servername
)
Write-Output "Server Name is valid"
}

输出:

[玩转系统] PowerShell 匹配 |匹配运算符在 PowerShell 中如何工作?

结论

当我们在 PowerShell 中处理字符串时,PowerShell Match 运算符非常有用,此外,为了验证输入表单,我们可以使用 Match 参数,这样我们就不需要编写冗长的代码。这些运算符降低了脚本的复杂性。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯