gpt4 book ai didi

powershell - -喜欢多种模式

转载 作者:行者123 更新时间:2023-12-04 00:58:41 25 4
gpt4 key购买 nike

是否有一种简单的方法(不使用循环)将 -like 多个模式应用于一个字符串?

所以

"c:\myfile.txt" -like "*.dat","*.txt"

应该返回 $true

"c:\myfile.dat" -like "*.dat","*.txt"

应该返回 $true

"c:\myfile.doc" -like ".dat",".txt"

应该返回 $false

最佳答案

我不这么认为。您可以使用正则表达式:

"c:\myfile.txt" -match '^.+\.(dat|txt)$'

更新

根据包含通配符模式的输入制作正则表达式模式:

    PS> $or = '"*.dat","*.txt","*.foo"' -replace '"|\*\.'  -replace ',','|'
PS> $pattern = '^.+({0})$' -f $or
PS> $pattern
^.+(dat|txt|foo)$

$string -match $pattern

关于powershell - -喜欢多种模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12941078/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com