提取字符串中文件扩展名(.ps1)之前的数字

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

创建文件的cmdlet返回一个文本块,这就是文件名。该文件的格式为string [number] .ps1,但是数字是随机的,因此我想提取该数字并将其存储在变量中。

供参考的文本块:

  +         ~~~~~\nA 'using' statement must appear before any other statements in\
    \ a script.\nAt C:\\Packages\\Plugins\\Microsoft.CPlat.Core.RunCommandWindows\\\
    1.1.3\\Downloads\\script35.ps1:20 char:8\n+         using Microsoft.Build.Framework;\n\
    +              ~\nMissing using directive\nAt C:\\Packages\\Plugins\\Microsoft.CPlat.Core.RunCommandWindows\\\
    1.1.3\\Downloads\\script35.ps1:20 char:3\n+         using Microsoft.Build.Framework;\n\
    +         ~~~~~\nA 'using' statement must appear before any other statements in\
    \ a script.\nAt C:\\Packages\\Plugins\\Microsoft.CPlat.Core.RunCommandWindows\\\
    1.1.3\\Downloads\\script35.ps1:21 char:8\n+         using Microsoft.Build.Utilities;\n\
    +              ~\nMissing using directive\nAt C:\\Packages\\Plugins\\Microsoft.CPlat.Core.RunCommandWindows\\\
    1.1.3\\Downloads\\script35.ps1:21 char:3\n+         using Microsoft.Build.Utilities;\n\
    +         ~~~~~\nA 'using' statement must appear before any other statements in\

我以为$result -match '\d\d+(?=\.\w+)'可以工作,但它只返回找到该数字的行,而不仅仅是返回数字。

regex powershell
1个回答
0
投票

使用collection(而不是单个值)作为LHS,即-match operator

  • 用作filter-即返回matching elements而不是布尔值的子数组

  • 是否不是用匹配的运算符结果填充-match

这就是为什么您的输出包含匹配行作为整体的原因。


[仅返回匹配的部分,可以将automatic $Matches variable$Matches选项一起使用来遍历行数组,在这种情况下,为每条输入线填充switch statement is

switch

以上产量:

-Regex

以上是最快的选择,但是如果您的收入来自大文件,而您又不想将其全部加载到内存中,则可以使用$Matches cmdlet:

# Array of sample lines.
# For illustrative purposes I've tweaked the `script<number>.ps1`
# tokens to have successive numbers.
$lines = @'
 +         ~~~~~\nA 'using' statement must appear before any other statements in\
    \ a script.\nAt C:\\Packages\\Plugins\\Microsoft.CPlat.Core.RunCommandWindows\\\
    1.1.3\\Downloads\\script35.ps1:20 char:8\n+         using Microsoft.Build.Framework;\n\
    +              ~\nMissing using directive\nAt C:\\Packages\\Plugins\\Microsoft.CPlat.Core.RunCommandWindows\\\
    1.1.3\\Downloads\\script36.ps1:20 char:3\n+         using Microsoft.Build.Framework;\n\
    +         ~~~~~\nA 'using' statement must appear before any other statements in\
    \ a script.\nAt C:\\Packages\\Plugins\\Microsoft.CPlat.Core.RunCommandWindows\\\
    1.1.3\\Downloads\\script37.ps1:21 char:8\n+         using Microsoft.Build.Utilities;\n\
    +              ~\nMissing using directive\nAt C:\\Packages\\Plugins\\Microsoft.CPlat.Core.RunCommandWindows\\\
    1.1.3\\Downloads\\script38.ps1:21 char:3\n+         using Microsoft.Build.Utilities;\n\
    +         ~~~~~\nA 'using' statement must appear before any other statements in\
'@ -split '\r?\n'

# Perform the matching, line by line,
# and output each line's matching part,
# resulting in an array of strings.
# Prepend 
#     $result = 
# to store the array in a variable.
switch -Regex ($lines) {
  '\d\d+(?=\.\w+)' { $Matches[0] }
}
© www.soinside.com 2019 - 2024. All rights reserved.