Powershell 循环通过多个值的行

Powershell 循环通过多个值的行,powershell,Powershell,Word文档内容:[(7)]/[(8)][担保代理行1]作为被担保方的担保受托人(“担保代理行”);及 要提取的值为“安全代理1” 它提取7,因为这是括号内的第一个 下面的代码工作正常,但只在括号内给出第一次出现/值。需要通过括号内的多个值进行循环,并给我括号内的第三个值 $FinalTable = Get-Content $SourceFile| select-string -pattern $SearchKeyword | Select -Property @

Word文档内容:[(7)]/[(8)][担保代理行1]作为被担保方的担保受托人(“担保代理行”);及

要提取的值为“安全代理1”

它提取7,因为这是括号内的第一个

下面的代码工作正常,但只在括号内给出第一次出现/值。需要通过括号内的多个值进行循环,并给我括号内的第三个值

$FinalTable = Get-Content $SourceFile|
        select-string -pattern $SearchKeyword |
        Select -Property @{Name = 'Name'; Expression = {$_.Line}}, @{Name = 'LineNo'; Expression = {$_.LineNumber}}, @{Name='Criteria';Expression = {$_.Category}} |
        ForEach-Object {

        $str = $_.Name
        $LineNumber = $_.LineNo
        $Criteria = $_.Criteria
        $start = $str.indexOf("[") + 1
        $end = $str.indexOf("]", $start)
        $length = $end - $start
        $result = ($str.substring($start, $length)).trim()

        #Creating a custom object to display in table format
        $Obj = New-Object -TypeName PSCustomObject
        Add-Member -InputObject $Obj -MemberType NoteProperty -Name Category -Value $Category
        Add-Member -InputObject $Obj -MemberType NoteProperty -Name Description -Value $result

        $obj
    } 
 $FinalTable | Export-Csv -Path $DestinationFile -NoTypeInformation -Encoding ASCII
按照西奥的建议尝试,但没有奏效

$FinalTable = Get-Content $SourceFile|
        select-string -pattern $SearchKeyword |
        Select -Property @{Name = 'Name'; Expression = {$_.Line}}, @{Name = 'LineNo'; Expression = {$_.LineNumber}}, @{Name='Criteria';Expression = {$_.Category}} |
        ForEach-Object {

        $str = $_.Name
        $LineNumber = $_.LineNo
        $Criteria = $_.Criteria

        #$start = $str.indexOf("[") + 1
        #$end = $str.indexOf("]", $start)
        #$length = $end - $start
        #$result = ($str.substring($start, $length)).trim()
                #Write-host $str
        if ($str -match '(\[[^\]]+])\/(\[[^\]]+])\s*\[\s*([^\]]+)]') {
            # $matches[1] --> "(7)"
            # $matches[2] --> "(8)"
            $result = $matches[3]  # --> "Security Agent 1"
            } 
        Write-Host $result
        #Creating a custom object to display in table format
        $Obj = New-Object -TypeName PSCustomObject
        Add-Member -InputObject $Obj -MemberType NoteProperty -Name Category -Value $Category
        Add-Member -InputObject $Obj -MemberType NoteProperty -Name Description -Value $result

        $obj
    } 
 $FinalTable | Export-Csv -Path $DestinationFile -NoTypeInformation -Encoding ASCII

您可以使用正则表达式获取字符串中括号之间的部分,如
[(7)]/[(8)][Security Agent 1]作为被担保方的担保受托人(“担保代理”)

而不是

$start = $str.indexOf("[") + 1
$end = $str.indexOf("]", $start)
$length = $end - $start
$result = ($str.substring($start, $length)).trim()


抱歉,我已经阅读了你问题的前5行,但我不明白你在说什么。在我的回答中编辑了正则表达式,因为第一行包含了
$matches[1]
$matches[2]
的方括号。我知道你不想要这些匹配,但我认为现在更好了。可能这不起作用,因为你在代码的其他地方犯了两个错误。1) 你应该使用
Select字符串模式([regex]::Escape($SearchKeyword))
,因为如果你的
$SearchKeyword
包含像
\[(
这样的字符,结果将不是你所期望的。2)您正在variablename
$条件下存储
$Category
值。之后,您将未定义的变量
$Category
添加到$Obj。这对我不起作用,结果输出中没有给我任何结果。我正在尝试更新有问题的代码now@user3657339这是您所描述内容的准确答案。$x.split(“[”)[2] .split(']')[0].trim()…此操作有效
if ($str -match '\[([^\]]+)\]\/\[([^\]]+)\]\s*\[\s*([^\]]+)]') {
    # $matches[1] --> "(7)"
    # $matches[2] --> "(8)"
    $result = $matches[3]  # --> "Security Agent 1"
} 
else {
    $result = ''   # should not happen..
}