我试图在数组中访问第一个捕获组([^ ]+)
的所有匹配,这样我就可以在输出中看到foreach
:
$input = "Row 1:
Computer: xxx
Last Heartbeat: 4/9/2020 11:27:24 AM
Row 2:
Computer: yyy
Last Heartbeat: 4/9/2020 11:27:37 AM"
$matches = ([regex]'Computer: ([^ ]+)').Matches($input)
$matches
产量:
Groups : {0, 1}
Success : True
Name : 0
Captures : {0}
Index : 7
Length : 13
Value : Computer: xxx
Groups : {0, 1}
Success : True
Name : 0
Captures : {0}
Index : 66
Length : 13
Value : Computer: yyy
诚然,我有很多关于数据结构和如何访问它们的知识要学。
发布于 2020-04-09 15:02:54
在我们找到真正的答案之前,请考虑重新命名变量-- $Matches
和$Input
都是https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_automatic_variables。
为了获取第一个捕获组的值,您需要在每个匹配的Groups
属性中定位索引1或在Captures
属性中寻址索引0:
$string = "Row 1:
Computer: xxx
Last Heartbeat: 4/9/2020 11:27:24 AM
Row 2:
Computer: yyy
Last Heartbeat: 4/9/2020 11:27:37 AM"
$results = ([regex]'Computer: ([^ ]+)').Matches($string)
$results | ForEach-Object { $_.Groups[1].Value }
# or
$results | ForEach-Object { $_.Captures[0].Value }
https://stackoverflow.com/questions/61124040
复制相似问题