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

[玩转系统] PowerShell 类似操作符 |了解 Like 运算符的示例

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

PowerShell 类似操作符 |了解 Like 运算符的示例


[玩转系统] PowerShell 类似操作符 |了解 Like 运算符的示例

PowerShell Like 运算符简介

PowerShell 中的 Like 运算符是匹配运算符的一种。匹配运算符用于使用正则表达式根据条件查找元素。 Like 和 not like 都是匹配运算符的类型。这些运算符主要用于识别一个字符串是否包含在另一个字符串中。为了比较不同的字符串,它们与通配符一起使用。当输入是单个标量对象时,会自动填充 $Matches 变量。对于标量输入,like 和 not like 运算符都返回一个布尔值,并且 $Matches 的值设置为相应的匹配组件。本文将详细介绍 like 和 not like 运算符。它还将涵盖其他字符串比较运算符,例如匹配、包含等。

语法:

like 和 notlike cmdlet 的语法如下。

<string[]> -match <regular-expression>
<string[]> -notmatch <regular-expression>

PowerShell Like 运算符的示例

以下是下面提到的示例

例子#1

输入:

Write-Host "Welcome to the example of like operator in PowerShell"
Write-Host "Like operator example without wildcard expression"
$input="My name is vignesh"
$input -like "sh"
Write-Host "like operator example with wildcard expression"
$input="My name is vignesh"
$input -like "*sh"

输出:

[玩转系统] PowerShell 类似操作符 |了解 Like 运算符的示例

在上面的示例中,没有通配符表达式在搜索完全匹配时会产生错误结果。使用通配符表达式,它仅搜索匹配项。

例子#2

输入:

Write-Host "Example of finding substring using like operator"
Write-Host "Finding sub string using wild card expression"
"abcde", "fghijk", "lmnop", "qrstuv", "wxyz" -like "abcd*"
Write-Host "finding sub string without wild card expression"
"abcde", "fghijk", "lmnop", "qrstuv", "wxyz" -like "abcde"

输出:

[玩转系统] PowerShell 类似操作符 |了解 Like 运算符的示例

例子#3

输入:

Write-Host "Welcome to the example of not like operator in PowerShell"
Write-Host " not Like operator example without wildcard expression"
$input="test input string"
$input -notlike "te"
Write-Host "not like operator example with wildcard expression"
$input="test input string"
$input -notlike "te*"

输出:

[玩转系统] PowerShell 类似操作符 |了解 Like 运算符的示例

例子#4

输入:

Write-Host "Example of finding substring using not like operator"
Write-Host "Finding sub string using wild card expression"
"one", "three", "seven", "ten", "tenth" -notlike "ten*"
Write-Host "finding sub string without wild card expression"
"adam", "john", "temp", "macho", "man" -notlike "ad"

输出:

[玩转系统] PowerShell 类似操作符 |了解 Like 运算符的示例

-匹配与不匹配:

匹配运算符用于使用正则表达式来匹配字符串。 $matches 变量会自动填充标量输入。这只能用于字符串,不能用于整数或任何其他数据对象。如果输入是集合,则仅返回输入中的匹配成员,并且不会填充 $matches 变量。如果输入是单个字符串,则填充 $matches 变量。

例子#5

输入:

write-host "Demo of match operator"
Write-Host "Match with wildcard charcaters for a single string"
$input="My name is vignesh"
$input -match "na*"
Write-Host "$matches is generated and assigned. The value is below"
$Matches
Write-Host "Match without wildcard charcaters for a single string"
$input -match "na"
Write-Host "$matches is generated and assigned. The value is below"
$Matches
Write-Host "Match with wildcard charcaters for a collection"
$input="abc", "abcd", "abdsf","erert"
$input -match "ab*"
Write-Host "$matches value is below"
$matches
Write-Host "Match without wildcard charcaters for a collection"
$input="abc", "abcd", "abdsf","erert"
$input -match "ab"
Write-Host "$matches value is below"
$matches

输出:

[玩转系统] PowerShell 类似操作符 |了解 Like 运算符的示例

在上面的示例中,您可以看到 $matches 不是为字符串集合生成的,而是仅为单个字符串输入生成的。

例子#6

输入:

write-host "Demo of not match operator"
Write-Host "Not Match with wildcard charcaters for a single string"
$input="My name is vignesh"
$input -notmatch "za*"
Write-Host "$matches is generated and assigned. The value is below"
$Matches
Write-Host "Not Match without wildcard charcaters for a single string"
$input -notmatch "za"
Write-Host "$matches is generated and assigned. The value is below"
$Matches
Write-Host "Not Match with wildcard charcaters for a collection"
$input="abc", "abcd", "abdsf","erert"
$input -notmatch "za*"
Write-Host "$matches value is below"
$matches
Write-Host "Not Match without wildcard charcaters for a collection"
$input="abc", "abcd", "abdsf","erert"
$input -notmatch "za"
Write-Host "$matches value is below"
$matches

输出:

[玩转系统] PowerShell 类似操作符 |了解 Like 运算符的示例

包含和不包含:

这些与 like 和 not like 运算符相同。这用于识别某个值是否存在于值集合中。这总是生成布尔值输出。仅当测试值与至少一个值完全匹配时,才返回 true。

语法:

<Reference-values> -contains <Test-value>

-不包含:

这与包含运算符相反。当没有完全匹配时返回 true。

语法:

<Reference-values> -notcontains <Test-value>

例子#7

输入:

Write-Host "Example of contains operator"
$input= "sdfsdfdsf","adsadsad","test","test1"
$input -contains "test"
$input= "sdfsdfdsf","adsadsad","test","test1"
$input -contains "dfsdf"
Write-Host "Demo of multiple check values"
$input="one","two","three","four","five"
$input -contains "one","two"
$input="one","two"
$input, "three","four" -ccontains $input

输出:

[玩转系统] PowerShell 类似操作符 |了解 Like 运算符的示例

例子#8

输入:

Write-Host "Example of not contains operator"
$input= "sdfsdfdsf","adsadsad","dfdf","sdf"
$input -notcontains "test"
$input= "sdfsdfdsf","adsadsad","test","test1"
$input -notcontains "test"
Write-Host "Demo of multiple check values"
$input="one","two","three","four","five"
$input -notcontains "one","two"
$input="one","two"
$input, "three","four" -notcontains $input

输出:

[玩转系统] PowerShell 类似操作符 |了解 Like 运算符的示例

例子#9

输入:

Write-Host "Example of finding substring using like operator"
Write-Host "Finding sub string using wild card expression"
"america", "australia", "antratica", "amesterdam", "angolio" -like "an*"
Write-Host "finding sub string without wild card expression"
"america", "australia", "antratica", "amesterdam", "angolio" -like "abcde"
Write-Host "Welcome to the example of not like operator in PowerShell"
Write-Host " not Like operator example without wildcard expression"
$input="one two three"
$input -notlike "te"
Write-Host "not like operator example with wildcard expression"
$input="my name is vignesh krishnakumar"
$input -notlike "te*"

输出:

[玩转系统] PowerShell 类似操作符 |了解 Like 运算符的示例

结论

因此,本文通过适当的示例详细解释了 like 运算符、其语法及其用法。本文还详细介绍了 not like 运算符、匹配和不匹配运算符、contains 和 not 运算符以及详细的解释和示例。使用示例程序显示了每种类型之间的差异,并提供了必要的解释。了解更多信息的最佳方法是练习示例程序并详细探索每个运算符,以便更好地了解何时使用哪个运算符,因为上面讨论的大多数运算符本质上都是相似的。

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

取消回复欢迎 发表评论:

关灯