1

So I have this line, well several.

<drives name="drive 1" deviceid="\\.\PHYSICALDRIVE0" interface="SCSI" totaldisksize="136,7">

and

<drives name="drive 1" deviceid="\\.\PHYSICALDRIVE0" interface="SCSI" totaldisksize="1367">

I have this line which match the the number between the quotes:

$(Select-String totaldisksize $Path\*.xml).line -replace '.*totaldisksize="(\d+,\d+)".*','$1'

Which will match 136,7 but not 1367.

Is it possible to match both?

Thank you.

user3019059
  • 187
  • 1
  • 17

2 Answers2

2

Sure:

$xmlfile = 'C:\path\to\output.xml'

& cscript sydi-server.vbs -t. -ex -o$xmlfile

[xml]$sysinfo = Get-Content $xmlfile

$driveinfo = $sysinfo.SelectNodes('//drives') |
             select name, @{n='DiskSize';e={[Double]::Parse($_.totaldisksize)}}

Note: Do not parse XML with regular expressions. It will corrupt your soul and make catgirls die in pain.

Community
  • 1
  • 1
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
0

Just make the , in your regex as optional by adding the ? quantifier next to ,.

$(Select-String totaldisksize $Path\*.xml).line -replace '.*totaldisksize="(\d+,?\d+)".*','$1'

OR

$(Select-String totaldisksize $Path\*.xml).line -replace '.*totaldisksize="(\d+(?:,\d+)*)".*','$1'

DEMO

Regular Expression:

(                        group and capture to \1:
  \d+                      digits (0-9) (1 or more times)
  (?:                      group, but do not capture (0 or more
                           times):
    ,                        ','
    \d+                      digits (0-9) (1 or more times)
  )*                       end of grouping
)                        end of \1
Avinash Raj
  • 172,303
  • 28
  • 230
  • 274