user3019059
user3019059

Reputation: 187

Find number between quotes

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.

Upvotes: 1

Views: 407

Answers (2)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200273

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.

Upvotes: 2

Avinash Raj
Avinash Raj

Reputation: 174706

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

Upvotes: 0

Related Questions