regex - 将 test-Path -Path 与正则表达式相结合
问题描述
在 Jenkins 中,我正在创建一个 PowerShell 脚本。这是有效的:
if (Test-Path -Path "$baseDirectory\s*") {...}
但这是“危险的”,不够“安全”。
目录名称将是:sdd 或 sddd (d=digits)。所以名称以字符“s”开头,长度为 2 位和后面的三位数字。
我尝试了以下方法:
if (Test-Path -Path "$baseDirectory\ -match 's(\d{2,3})'") {...}
和
if (Test-Path -Path "-match '$baseDirectory\s(\d{2,3})'") {...}
和所有的小差异。
如果我使用 s* 执行此操作,则脚本将返回目录名称 s54、s55 等。
如果可能的话,我希望使用正则表达式而不是“找不到目录”得到相同的结果。
解决方案
您可以使用它来查看是否可以找到具有该名称的一个或多个文件夹。
if ([bool](Get-ChildItem "$baseDirectory\s*" -Directory | Where-Object { $_.Name -match 's\d{2,3}$' })) {
# your code goes here
}
希望有帮助
推荐阅读
- pygame - 在pygame中重新启动游戏时计时器无法正确启动且未重置
- java - 更改时间选择器会导致应用程序崩溃
- sass - 带有 sass 映射的 Sass 错误错误:预期的“;”
- mysql - MySQL 在 SELECT 查询上添加行
- typescript - 在 TypeScript 中将两个数组合并为一个
- php - 从json_encode嵌套数组php中的手镯中删除双引号
- javascript - onSubmitEditing 永远不会触发?
- vb.net - 动态调用不是控件的对象
- java - 如何在 Java 中发现多播地址范围?
- android - 如何在我的 AppbarLayout 之后放置我的 NestedScrollView