我正在捕获输出并对其执行正则表达式,以确定字符串“脚本”后面的数字。我正在使用regex‘script\\d’,但是它返回的输出比我预期的要多。
结果输出示例:
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'
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\
我只是试图捕捉字符串“脚本”的第一次迭代,以及它之后出现的任何数字,但似乎无法让它工作。
发布于 2020-01-26 04:40:16
由于$result
是一个行数组,-match
将返回与regex模式匹配的行。如果您匹配单个项而不是集合,$matches
自动变量将包含匹配的字符串和任何相应的捕获组。
因此,为了以这种方式使用-match
,您需要逐行处理$result
。我倾向于使用switch
语句来有效地完成同样的任务。
switch -Regex ($result) {
'script(\d+)' { $matches.1 ; break }
}
break语句将在找到第一个匹配之后停止处理匹配。
发布于 2020-01-26 04:45:45
Windows 10 64位。PowerShell 5.1
结果输出到output.txt
# 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
结果:
11
如果-match返回整行(充当筛选器),请使用gc -raw
不带Get-Content
的-Raw
返回一行数组,并将数组作为第一个参数传递给.Match()
,从而使PowerShell将数组元素与空格连接起来,从而产生假阳性;例如,[regex]::Match(('C:\Program', 'Files'), 'Program Files')
匹配。
默认情况下,.NET正则表达式是区分大小写的,而PowerShell正则表达式是不区分大小写的.
https://stackoverflow.com/questions/59915131
复制相似问题