Powershell中的Cast[regex]会导致区分大小写的奇怪-为什么?
我是Powershell脚本的新手,我对自己遇到的一些行为感到困惑。如果这个问题是愚蠢的,请提前道歉 我一直在下面的例子中学习如何使用匹配,并且看到了一些将[regex]放在字符串前面的例子。我假设(可能是错误的)这是一个强制转换,明确指定对象是正则表达式。但使用[regex]似乎会改变结果匹配的大小写敏感性:Powershell中的Cast[regex]会导致区分大小写的奇怪-为什么?,regex,powershell,Regex,Powershell,我是Powershell脚本的新手,我对自己遇到的一些行为感到困惑。如果这个问题是愚蠢的,请提前道歉 我一直在下面的例子中学习如何使用匹配,并且看到了一些将[regex]放在字符串前面的例子。我假设(可能是错误的)这是一个强制转换,明确指定对象是正则表达式。但使用[regex]似乎会改变结果匹配的大小写敏感性: PS > $array = 'ABC','DEF','GHI' PS > $array -match 'DEF' DEF PS > $array -match 'def
PS > $array = 'ABC','DEF','GHI'
PS > $array -match 'DEF'
DEF
PS > $array -match 'def'
DEF
PS > $array -match [regex]'def'
PS > $array -match [regex]'DEF'
DEF
这里到底发生了什么?[regex]在做什么,导致区分大小写
[regex]'def'
正在将字符串强制转换为对象,该对象默认区分大小写。另一方面,PowerShell正则表达式默认情况下不区分大小写
PS C:\> ('def').GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
PS C:\> ([regex]'def').GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Regex System.Object
PS C:\>('def').GetType()
IsPublic IsSerial名称基类型
-------- -------- ---- --------
真字符串系统。对象
PS C:\>([regex]'def').GetType()
IsPublic IsSerial名称基类型
-------- -------- ---- --------
True Regex System.Object
[Regex]'def'
将字符串强制转换为对象,默认情况下该对象区分大小写。另一方面,PowerShell正则表达式默认情况下不区分大小写
PS C:\> ('def').GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
PS C:\> ([regex]'def').GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Regex System.Object
PS C:\>('def').GetType()
IsPublic IsSerial名称基类型
-------- -------- ---- --------
真字符串系统。对象
PS C:\>([regex]'def').GetType()
IsPublic IsSerial名称基类型
-------- -------- ---- --------
True Regex System.Object
默认情况下,$array-match
调用不区分大小写
PS C:\> ('def').GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
PS C:\> ([regex]'def').GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Regex System.Object
添加
[regex]
关键字将启动正则表达式搜索,默认情况下,正则表达式区分大小写。仅此而已。默认情况下,$array-match
调用不区分大小写
PS C:\> ('def').GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True String System.Object
PS C:\> ([regex]'def').GetType()
IsPublic IsSerial Name BaseType
-------- -------- ---- --------
True True Regex System.Object
添加[regex]
关键字将启动正则表达式搜索,默认情况下,正则表达式区分大小写。就这些