我有一个字符串:$row.TableName_Org[$i]
它包含的值是
This is a happy little asterisk: '*'
Run Code Online (Sandbox Code Playgroud)
现在我想根据字符串包含星号的事实来执行 IF 操作。
if($row.TableName_Org[$i] -Match "*") {
//Do Something
}
Run Code Online (Sandbox Code Playgroud)
但是给了我这个错误:
"*" - Kwantiteitsmeter {x,y} wordt door niets voorafgegaan. parseren
At C:\Users\hveijer\VS Code Repos\migratie-uitwissel\ReadData.ps1:33 char:4
+ $row.TableName_Org[$i] -match "*"
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : OperationStopped: (:) [], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException
Run Code Online (Sandbox Code Playgroud)
正如您自己发现的那样,PowerShells 通配符/全局机制中的转义字符是`(反引号):
'string with * in it' -like '`*'
Run Code Online (Sandbox Code Playgroud)
...但反引号也是可扩展(双引号)字符串的转义字符,导致出现尴尬的双转义情况,例如:
$pattern = "${prefix}``*"
Run Code Online (Sandbox Code Playgroud)
出于这个原因,我更喜欢让 PowerShell 帮我转义我的搜索词,而不是手动执行:
[wildcardpattern]::Escape("${prefix}*")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2918 次 |
| 最近记录: |