Powershell 全局模式匹配

2024-01-22

我正在看C:\ProgramFiles对于一个名为log4j-core-x.y.z.jar。我正在尝试匹配最后一位数字z,可以是一位数也可以是两位数 (0-99)。我似乎无法获得正确的全局模式来完成此任务。

Code:

PS C:\Users\Administrator> Get-ChildItem -Path 'C:\Program Files\' -Filter log4j-core-*.*.[1-9][0-9].jar -Recurse -ErrorAction SilentlyContinue -Force | %{$_.FullName}

这不会产生任何结果,但是当我只执行所有通配符时,-Filter log4j-core-*.*.*.jar, I get:

C:\Program Files\apache-log4j-2.16.0-bin\apache-log4j-2.16.0-bin\log4j-core-2.16.0-javadoc.jar
C:\Program Files\apache-log4j-2.16.0-bin\apache-log4j-2.16.0-bin\log4j-core-2.16.0-sources.jar
C:\Program Files\apache-log4j-2.16.0-bin\apache-log4j-2.16.0-bin\log4j-core-2.16.0-tests.jar
C:\Program Files\apache-log4j-2.16.0-bin\apache-log4j-2.16.0-bin\log4j-core-2.16.0.jar

我唯一关心的是得到C:\Program Files\apache-log4j-2.16.0-bin\apache-log4j-2.16.0-bin\log4j-core-2.16.0.jar, log4j-core-2.16.0.jar


-Filter不支持过滤regex or 字符范围 https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_regular_expressions?view=powershell-7.2#character-ranges例如[A-Z] or [0-9]。谢谢马克门0 https://stackoverflow.com/questions/70355772/powershell-glob-pattern-matching/70356061?noredirect=1#comment124370165_70356061指出这一点。

从参数说明来看Get-ChildItem https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.management/get-childitem?view=powershell-7.2官方文档:

过滤器字符串被传递到 .NET API 以枚举文件。该API仅支持* and ?通配符。

尝试用这个:

$getChildItemSplat = @{
    Path        = 'C:\Program Files\'
    Filter      = 'log4j-core-*.*.??.jar'
    Recurse     = $true
    ErrorAction = 'SilentlyContinue'
    Force       = $true
}

Get-ChildItem @getChildItemSplat |
    # Ends with a . followed by 1 or 2 digits and the .jar extension
    Where-Object Name -Match '\.\d{1,2}\.jar$'
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Powershell 全局模式匹配 的相关文章

随机推荐