[玩转系统] PowerShell 类似操作符 |了解 Like 运算符的示例
作者:精品下载站 日期:2024-12-14 04:57:49 浏览:16 分类:玩电脑
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"
输出:
在上面的示例中,没有通配符表达式在搜索完全匹配时会产生错误结果。使用通配符表达式,它仅搜索匹配项。
例子#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"
输出:
例子#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*"
输出:
例子#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"
输出:
-匹配与不匹配:
匹配运算符用于使用正则表达式来匹配字符串。 $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
输出:
在上面的示例中,您可以看到 $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
输出:
包含和不包含:
这些与 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
输出:
例子#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
输出:
例子#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*"
输出:
结论
因此,本文通过适当的示例详细解释了 like 运算符、其语法及其用法。本文还详细介绍了 not like 运算符、匹配和不匹配运算符、contains 和 not 运算符以及详细的解释和示例。使用示例程序显示了每种类型之间的差异,并提供了必要的解释。了解更多信息的最佳方法是练习示例程序并详细探索每个运算符,以便更好地了解何时使用哪个运算符,因为上面讨论的大多数运算符本质上都是相似的。
猜你还喜欢
- 03-30 [玩转系统] 如何用批处理实现关机,注销,重启和锁定计算机
- 02-14 [系统故障] Win10下报错:该文件没有与之关联的应用来执行该操作
- 01-07 [系统问题] Win10--解决锁屏后会断网的问题
- 01-02 [系统技巧] Windows系统如何关闭防火墙保姆式教程,超详细
- 12-15 [玩转系统] 如何在 Windows 10 和 11 上允许多个 RDP 会话
- 12-15 [玩转系统] 查找 Exchange/Microsoft 365 中不活动(未使用)的通讯组列表
- 12-15 [玩转系统] 如何在 Windows 上安装远程服务器管理工具 (RSAT)
- 12-15 [玩转系统] 如何在 Windows 上重置组策略设置
- 12-15 [玩转系统] 如何获取计算机上的本地管理员列表?
- 12-15 [玩转系统] 在 Visual Studio Code 中连接到 MS SQL Server 数据库
- 12-15 [玩转系统] 如何降级 Windows Server 版本或许可证
- 12-15 [玩转系统] 如何允许非管理员用户在 Windows 中启动/停止服务
取消回复欢迎 你 发表评论:
- 精品推荐!
-
- 最新文章
- 热门文章
- 热评文章
[影视] 黑道中人 Alto Knights(2025)剧情 犯罪 历史 电影
[古装剧] [七侠五义][全75集][WEB-MP4/76G][国语无字][1080P][焦恩俊经典]
[实用软件] 虚拟手机号 电话 验证码 注册
[电视剧] 安眠书店/你 第五季 You Season 5 (2025) 【全10集】
[电视剧] 棋士(2025) 4K 1080P【全22集】悬疑 犯罪 王宝强 陈明昊
[软件合集] 25年6月5日 精选软件22个
[软件合集] 25年6月4日 精选软件36个
[短剧] 2025年06月04日 精选+付费短剧推荐33部
[短剧] 2025年06月03日 精选+付费短剧推荐25部
[软件合集] 25年6月3日 精选软件44个
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电视剧] 欢乐颂.5部全 (2016-2024)
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[影视] 【稀有资源】香港老片 艺坛照妖镜之96应召名册 (1996)
[剧集] 神经风云(2023)(完结).4K
[剧集] [BT] [TVB] [黑夜彩虹(2003)] [全21集] [粤语中字] [TV-RMVB]
[实用软件] 虚拟手机号 电话 验证码 注册
[资源] B站充电视频合集,包含多位重量级up主,全是大佬真金白银买来的~【99GB】
[影视] 内地绝版高清录像带 [mpg]
[书籍] 古今奇书禁书三教九流资料大合集 猎奇必备珍藏资源PDF版 1.14G
[电视剧] [突围] [45集全] [WEB-MP4/每集1.5GB] [国语/内嵌中文字幕] [4K-2160P] [无水印]
[剧集] [央视][笑傲江湖][2001][DVD-RMVB][高清][40集全]李亚鹏、许晴、苗乙乙
[电影] 美国队长4 4K原盘REMUX 杜比视界 内封简繁英双语字幕 49G
[电影] 死神来了(1-6)大合集!
[软件合集] 25年05月13日 精选软件16个
[精品软件] 25年05月15日 精选软件18个
[绝版资源] 南与北 第1-2季 合集 North and South (1985) /美国/豆瓣: 8.8[1080P][中文字幕]
[软件] 25年05月14日 精选软件57个
[短剧] 2025年05月14日 精选+付费短剧推荐39部
[短剧] 2025年05月15日 精选+付费短剧推荐36部
- 最新评论
-
- 热门tag