Powershell中圆括号和方括号的转义字符

escape character for round and square brackets in powershell

我试图用包含圆括号和方括号的定界符拆分一个数组,但是我尝试了不同的技巧组合但没有奏效。 这是我要处理的一维数组

$数组

process-a.exe[80]: in process 
process-b.exe[30]: failed:retrying
process-c.exe[40]: in process 
process-y.exe:(sub-process-e.exe[20]): failed:suspended
process-z.exe:(sub-process-f.exe[30]): in process 
process-g.exe[10]: completed

我希望这是拆分后的二维数组

预期结果

process name                                p_id    status
------------------------------------------------
process-a.exe[80]:                          80      in process
process-b.exe[30]:                          30      failed:retrying
process-c.exe[40]:                          40      in process
process-y.exe[1]:(sub-process-e.exe[20]):   20      failed:suspended
process-z.exe[1]:(sub-process-f.exe[30]):   30      in process
process-g.exe[10]:                          10      completed

这是我试过的东西

1.转义字符

反斜杠\

$proc = $proc | % { $_ -split '\]:'}

回勾`

$proc = $proc | % { $_ -split '`]:'}

2。引用

单引号 ''

$proc = $proc | % { $_ -split '`]:'}

双引号 ""

$proc = $proc | % { $_ -split "\]:"}

3。方法

$delim1 = '):'
$delim2 = ']:'

正则表达式

[System.Text.RegularExpressions.Regex]::Escape($delim1)

通配符

[WildcardPattern]::Escape($demin1)

由于您尝试在多个列中使用数据,拆分似乎不是最佳方法。正则表达式应该可以帮助您更轻松地获得所需的结果。

字符串数组

$array = 'process-a.exe[80]: in process',
         'process-b.exe[30]: failed:retrying',
         'process-c.exe[40]: in process',
         'process-y.exe:(sub-process-e.exe[20]): failed:suspended',
         'process-z.exe:(sub-process-f.exe[30]): in process',
         'process-g.exe[10]: completed'

带有正则表达式模式匹配的 Switch 语句

switch -Regex ($array){
    '^(.+\[(.+)\].{0,}:) (.+)$' {
        [PSCustomObject]@{
            'process name' = $matches.1
            p_id           = $matches.2
            status         = $matches.3
        }
    }
}

输出

process name                           p_id status          
------------                           ---- ------          
process-a.exe[80]:                     80   in process      
process-b.exe[30]:                     30   failed:retrying 
process-c.exe[40]:                     40   in process      
process-y.exe:(sub-process-e.exe[20]): 20   failed:suspended
process-z.exe:(sub-process-f.exe[30]): 30   in process      
process-g.exe[10]:                     10   completed   

正则表达式解释

^行首

() 捕获组

.+匹配一个或多个任意字符

\[ 文字方括号,转义

.{0,} 匹配零个或多个任意字符

$行尾

如果此文本在文件中,则只需将开关更改为

switch -Regex -File ($filepath){
    '^(.+\[(.+)\].{0,}:) (.+)$' {
        [PSCustomObject]@{
            'process name' = $matches.1
            p_id           = $matches.2
            status         = $matches.3
        }
    }
}

您可以通过将变量赋值放在 switch 语句之前,将所有开关输出分配给一个变量。

编辑

根据您的评论以及最好添加到您的 post 中的额外详细信息,我进行了以下调整

switch -Regex ($array){
    '^(.+\[(.+)\].+?) (.+)$' {
        [PSCustomObject]@{
            'process name' = $matches.1
            p_id           = $matches.2
            status         = $matches.3
        }
    }
}

输出

process name                                                  p_id  status                                                               
------------                                                  ----  ------                                                               
process-a.exe[80]:                                            80    in process                                                           
process-b.exe[30]:                                            30    failed:retrying                                                      
process-c.exe[40]:                                            40    in process                                                           
process-y.exe:(sub-process-e.exe[20]):                        20    failed:suspended                                                     
process-z.exe:(sub-process-f.exe[30]):                        30    in process                                                           
process-g.exe[10]:                                            10    completed                                                            
com.apple.xpc.launchd[1] (com.apple.mdworker.bundles[53136]): 53136 Could not find uid associated with service: 0: Undefined error: 0 501