选择花括号内的所有内容

时间:2017-09-23 15:28:06

标签: regex powershell

我们可以得到结果

edf560af-db87-11e3-80d2-806e6f6e6963
faadf429-db69-11e3-919f-e0db55bf0b1b

尝试

(Get-WmiObject Win32_Volume |select  deviceid) -replace '\D'
# removes all except numbers

这是cmd

Get-WmiObject Win32_Volume |select  deviceid

deviceid                                                                                                                                                                               
--------                                                                                                                                                                               
\\?\Volume{edf560af-db87-11e3-80d2-806e6f6e6963}\                                                                                                                                      
\\?\Volume{faadf429-db69-11e3-919f-e0db55bf0b1b}\                                                                                                                                      
\\?\Volume{faadf42d-db69-11e3-919f-e0db55bf0b1b}\                                                                                                                                      
\\?\Volume{faadf433-db69-11e3-919f-e0db55bf0b1b}\                                                                                                                                      
\\?\Volume{faadf439-db69-11e3-919f-e0db55bf0b1b}\                                                                                                                                      
\\?\Volume{edf560b0-db87-11e3-80d2-806e6f6e6963}\                                                                                                                                      
\\?\Volume{edf560b1-db87-11e3-80d2-806e6f6e6963}\                                                                                                                                      
\\?\Volume{edf560b5-db87-11e3-80d2-806e6f6e6963}\  

3 个答案:

答案 0 :(得分:0)

试试这个正则表达式:

(?<=(Volume{))([\s\S]| |w\[0-9]| )+?(?=(}))
or simpler
(?<=Volume{).+(?=})

https://regex101.com/r/MHnxxh/4 要么 https://regex101.com/r/MHnxxh/5

答案 1 :(得分:0)

即使您尝试的变体也无法使用,因为它还会包含e的数量:

PS> (Get-WmiObject Win32_Volume |select  -expand deviceid) -replace '[^0-9a-f]'
ed0addcdf000000000000100000000000
ed0addcdf000000000000501f00000000
e775fc6ae9c7511e7b58e10604b928269
e2367fe0e45e311e6b548806e6f6e6963

这是另一个使用lookarounds的工作解决方案:

Get-WmiObject Win32_Volume | Select-String '(?<=\{)[0-9a-f\-]+(?=\})' | ForEach-Object {$_.matches.value}

答案 2 :(得分:0)

您似乎只想捕捉LEFT CURLY BRACKET和RIGHT CURLY BRACKET之间的十六进制数字。

PS C:\src\t> cat .\dsm.ps1
Get-CimInstance CIM_StorageVolume |
    Select-Object DeviceID |
    ForEach-Object { $_ -match ".*{([-0-9a-fA-F]*)}.*" } |
    ForEach-Object { $matches[1] -replace '-' }

输出结果为:

PS C:\src\t> .\dsm.ps1
04ec358ff8bc11e3bac6806e6f6e6963
04ec3590f8bc11e3bac6806e6f6e6963
2bd2c32a00000000000090b4e8000000
c0adb44bbbcc11e5b3e7806e6f6e6963