麻烦地在Powershell中使用-like匹配单词

问题描述 投票:1回答:2

我的代码工作不正常,它应该采用浮动的“this”或“that”并决定它是否与上述单词之一相匹配,但它目前停在第一个if语句并说所有单词都匹配,他们绝对没有。我试过-match-eq-like ....我在错误的线前面放了一个“x”。这只是一个更大代码的短线。 (BRC - 批量重命名实用程序(命令行))

if ($this){
    #exceptions
    if ($that -like "Drücker" -or "Biegekern" -or "Führung" -or "Pressentisch"){Write-Host ("x($i/$rowMax) Replacing $that with $this")
        .\BRC32 /DIR:$directory /INCLR /RECURSIVE /quiet /IGNOREFILEX /REPLACECI:"3_$that":"3_$this ($that)" /REPLACECI:_Dr.Pos.:"_$this ($that)_Pos_" /replaceci:" ":_ /execute
    }
    elseif ($that -like "Halteplatte" -or "Oberteil" -or "Unterteil" -or "Schieber"){Write-Host ("($i/$rowMax) Replacing $that with $this")
        .\BRC32 /DIR:$directory /INCLR /RECURSIVE /quiet /IGNOREFILEX /REPLACECI:"3_$that":"3_$this ($that)" /replaceci:" ":_ /execute
        .\BRC32 /DIR:$directory /INCLR /RECURSIVE /quiet /IGNOREFILEX /REPLACECI:"2_$that":"2_$this ($that)" /replaceci:" ":_ /execute
    }
    elseif ($that -like "Schnitt"){Write-Host ("($i/$rowMax) Replacing $that with $this")
        .\BRC32 /DIR:$directory /INCLR /RECURSIVE /quiet /IGNOREFILEX /REPLACECI:"0_$that":"0_$this ($that)" /replaceci:" ":_ /execute
    }
    elseif ($that -like "Zusatzführung"){Write-Host ("($i/$rowMax) Replacing $that with $this")
        .\BRC32 /DIR:$directory /INCLR /RECURSIVE /quiet /IGNOREFILEX /REPLACECI:"2_$that":"2_$this ($that)" /replaceci:" ":_ /execute
    }

    #main renaming function
    elseif ($that){
        Write-Host ("($i/$rowMax) Replacing $that with $this")
        .\BRC32 /DIR:$directory /INCLR /RECURSIVE /quiet /IGNOREFILEX /REPLACECI:"_$that":"_$this ($that)" /replaceci:" ":_ /execute
    }
}
elseif ($that){
    Write-Host ("($i/$rowMax) Blank-Skip")
    $blanks=$blanks+1
}
else {
    $i=$rowMax
}
powershell if-statement string-matching
2个回答
2
投票

呃人犯错误。我做吨!最好的学习方式。因为将$that与这样的东西相匹配的多个标准可能更合适。简单的正则表达式具有相同的逻辑

if ($that -match "(Drücker|Biegekern|Führung|Pressentisch)"){
    Do-Stuff()
}

如果你对匹配的反应不同,那么开关会更合适。

switch ($that) 
    { 
        Drücker {"wo sind meine Hosen."} 
        Biegekern {"meinem Hologramm ist voller Ohren."} 
        Führung {"mein Deutsch ist nicht gut."} 
        Pressentisch {"Zwei."} 
        default {"keine Ahnung."}
    }

其他更复杂的开关示例可以帮助找到here。使用上述两个片段的示例就是这样

$that = "Zusatzführung"

switch -Regex ($that) 
    { 
        "^(Drücker|Biegekern|Führung|Pressentisch)$" {"wo sind meine Hosen."} 
        "^(Halteplatte|Oberteil|Unterteil|Schieber)$" {"meinem Hologramm ist voller Ohren."} 
        "Schnitt" {"mein Deutsch ist nicht gut."} 
        "Zusatzführung" {"Zwei."} 
        default {"keine Ahnung."}
    }

在这种情况下,$that将匹配第一个条目。如果您正在寻找完整的单词匹配,您可以像我一样使用^$。您还可以使用break关键字来防止多个条目匹配。

....
"(Drücker|Biegekern|Führung|Pressentisch)" {"wo sind meine Hosen."; Break} 
"Zusatzführung" {"Zwei."}
....

使用与上述行相同的$that只会匹配第一行。然后在输出线之后我们退出开关。


0
投票

简单地说,要完成这项工作,你需要重写 - 和 - 或。我只是个dingus

if ($that -like "Drücker" -or $that -like "Biegekern" -or $that -like "Führung" -or $that -like "Pressentisch"){Write-Host ("x($i/$rowMax) Replacing $that with $this")
                    .\BRC32 /DIR:$directory /INCLR /RECURSIVE /quiet /IGNOREFILEX /REPLACECI:"3_$that":"3_$this ($that)" /REPLACECI:_Dr.Pos.:"_$this ($that)_Pos_" /replaceci:" ":_ /execute
                }
© www.soinside.com 2019 - 2024. All rights reserved.