Powershell - Regular Expression Multiple Matches

后端 未结 4 2083
庸人自扰
庸人自扰 2020-12-17 21:57

Maybe my reasoning is faulty, but I can\'t get this working.

Here\'s my regex: (Device\\s#\\d(\\n.*)*?(?=\\n\\s*Device\\s#|\\Z))

Try it: http://

4条回答
  •  天命终不由人
    2020-12-17 22:42

    While it doesn't exactly answer your question, I'll offer a slightly different approach:

    ($getdevice)  -split '\s+(?=Device #\d)' | select -Skip 1
    

    Just for fun,

    $drives = 
    ($getdevice)  -split '\s+(?=Device #\d)' | 
    select -Skip 1 |
    foreach { $Stringdata = 
              $_.replace(' : ','=') -replace 'Device #(\d)','Device = $1' -Replace 'Device is a (\w+)','DeviceIs = $1'
              New-Object PSObject -Property  $(ConvertFrom-StringData $Stringdata)
              } 
    
    $drives | select Device,DeviceIs,'Total Size'
    
    Device                             DeviceIs                           Total Size                        
    ------                             --------                           ----------                        
    0                                  Hard drive                         70007 MB                          
    1                                  Hard drive                         70007 MB                          
    2                                  Hard drive                         286102 MB                         
    3                                  Hard drive                         286102 MB                
    

提交回复
热议问题