首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >PowerShell中的Regex返回比需要多的信息

PowerShell中的Regex返回比需要多的信息
EN

Stack Overflow用户
提问于 2020-01-26 02:42:28
回答 2查看 88关注 0票数 0

我正在捕获输出并对其执行正则表达式,以确定字符串“脚本”后面的数字。我正在使用regex‘script\\d’,但是它返回的输出比我预期的要多。

结果输出示例:

代码语言:javascript
运行
复制
value:
- code: ComponentStatus/StdOut/succeeded
  displayStatus: Provisioning succeeded
  level: Info
  message: ''
  time: null
- code: ComponentStatus/StdErr/succeeded
  displayStatus: Provisioning succeeded
  level: Info
  message: "At C:\\Packages\\Plugins\\Microsoft.CPlat.Core.RunCommandWindows\\1.1.3\\\
    Downloads\\script11.ps1:9 char:98\n+ ... \\Microsoft.Net\\Framework\\v4.0.30319\\\
    Microsoft.Build.Tasks.v4.0.dll\" >\n+                                        \
    \                                  ~\nMissing file specification after redirection\
    \ operator.\nAt C:\\Packages\\Plugins\\Microsoft.CPlat.Core.RunCommandWindows\\\
    1.1.3\\Downloads\\script11.ps1:13 char:9\n+             using System;\n+     \
    \             ~\nMissing using directive\nAt C:\\Packages\\Plugins\\Microsoft.CPlat.Core.RunCommandWindows\\\
    1.1.3\\Downloads\\script11.ps1:13 char:4\n+             using System;\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\\script11.ps1:14 char:9\n+             using System.Reflection;\n\
    +                  ~\nMissing using directive\nAt C:\\Packages\\Plugins\\Microsoft.CPlat.Core.RunCommandWindows\\\
    1.1.3\\Downloads\\script11.ps1:14 char:4\n+             using System.Reflection;\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\\script11.ps1:15 char:9\n+             using System.Diagnostics;\n\
    +                  ~\nMissing using directive\nAt C:\\Packages\\Plugins\\Microsoft.CPlat.Core.RunCommandWindows\\\
    1.1.3\\Downloads\\script11.ps1:15 char:4\n+             using System.Diagnostics;\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\\script11.ps1:16 char:9\n+             using System.Runtime.InteropServices;\n\
    +                  ~\nMissing using directive\nAt C:\\Packages\\Plugins\\Microsoft.CPlat.Core.RunCommandWindows\\\
    1.1.3\\Downloads\\script11.ps1:16 char:4\n+             using System.Runtime.InteropServices;\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\\script11.ps1:17 char:9\n+             using Microsoft.Build.Framework;\n\
    +                  ~\nMissing using directive\nNot all parse errors were reported.\
    \  Correct the reported errors and try again.\n    + CategoryInfo          : ParserError:\
    \ (:) [], ParentContainsErrorRecordException\n    + FullyQualifiedErrorId : MissingFileSpecification\n\
    \ "
  time: null

regex输出示例

$result -match 'script\d\d'

代码语言:javascript
运行
复制
    Downloads\\script11.ps1:9 char:98\n+ ... \\Microsoft.Net\\Framework\\v4.0.30319\\\
    1.1.3\\Downloads\\script11.ps1:13 char:9\n+             using System;\n+     \
    1.1.3\\Downloads\\script11.ps1:13 char:4\n+             using System;\n+     \
    1.1.3\\Downloads\\script11.ps1:14 char:9\n+             using System.Reflection;\n\
    1.1.3\\Downloads\\script11.ps1:14 char:4\n+             using System.Reflection;\n\
    1.1.3\\Downloads\\script11.ps1:15 char:9\n+             using System.Diagnostics;\n\
    1.1.3\\Downloads\\script11.ps1:15 char:4\n+             using System.Diagnostics;\n\
    1.1.3\\Downloads\\script11.ps1:16 char:9\n+             using System.Runtime.InteropServices;\n\
    1.1.3\\Downloads\\script11.ps1:16 char:4\n+             using System.Runtime.InteropServices;\n\
    1.1.3\\Downloads\\script11.ps1:17 char:9\n+             using Microsoft.Build.Framework;\n\

我只是试图捕捉字符串“脚本”的第一次迭代,以及它之后出现的任何数字,但似乎无法让它工作。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-01-26 04:40:16

由于$result是一个行数组,-match将返回与regex模式匹配的行。如果您匹配单个项而不是集合,$matches自动变量将包含匹配的字符串和任何相应的捕获组。

因此,为了以这种方式使用-match,您需要逐行处理$result。我倾向于使用switch语句来有效地完成同样的任务。

代码语言:javascript
运行
复制
switch -Regex ($result) {
    'script(\d+)' { $matches.1 ; break }
}

break语句将在找到第一个匹配之后停止处理匹配。

票数 2
EN

Stack Overflow用户

发布于 2020-01-26 04:45:45

Windows 10 64位。PowerShell 5.1

结果输出到output.txt

代码语言:javascript
运行
复制
# Read file as a whole, into a single, multi-line string. Prevents -match acting as a filter
$z = gc -raw output.txt
[regex]::Match($z,'script(\d+)').Groups[1].value

结果:

代码语言:javascript
运行
复制
11

理解PowerShell中的Regex捕获组

如果-match返回整行(充当筛选器),请使用gc -raw

不带Get-Content-Raw返回一行数组,并将数组作为第一个参数传递给.Match(),从而使PowerShell将数组元素与空格连接起来,从而产生假阳性;例如,[regex]::Match(('C:\Program', 'Files'), 'Program Files')匹配。

默认情况下,.NET正则表达式是区分大小写的,而PowerShell正则表达式是不区分大小写的.

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59915131

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档