Rakha
Rakha

Reputation: 2064

Extract version number from string, optimal way?

I have that string:

<span class="cloud-browser-downloads__dl-row-chrome-container cloud-body-text__small">Chrome v. <span class="cloud-browser-downloads__dl-row-chrome-version">74.0.3729.131</span></span>

I just want the version number. I'm wondering what you guys think would be the optimal way to isolate it.

Right now i'm doing it via REGEX like this :

-match "\d{1,}[\.]\d{1,}[\.]\d{1,}[\.]\d{1,}"

(1 or more Numbers, dot, 1 or more Numbers, dot, 1 or more Numbers, dot, 1 or more Numbers)

It works perfectly however what if they were to change the version naming?

Another way to see it would be to isolate what is between

<span class="cloud-browser-downloads__dl-row-chrome-version">

and

</span>

Just looking for your opinion on the safest way to approach this.

FYI : I know I can use Invoke-Webrequest to fetch the actual element but for some reason that command hangs on this particular google URL so I had to make do with just fetching the source code with an InternetExplorer.Application COM objet.

Upvotes: 2

Views: 291

Answers (2)

Anivia
Anivia

Reputation: 53

You can use with -match .

$str =  "<span class=cloud-browser-downloads__dl-row-chrome-container cloud-body- 
text__small>Chrome v. <span class=cloud-browser-downloads__dl-row-chrome- 
  version>74.0.3729.131</span></span>"
 $regex = [regex] "\d{1,}[\.]\d{1,}[\.]\d{1,}[\.]\d{1,}"

$found = $str -match $regex

if ($found) {
 $ip = $matches[0]
}

Upvotes: 1

user6811411
user6811411

Reputation:

Cast your string to xml:

$XML = [XML]'<span class="cloud-browser-downloads__dl-row-chrome-container cloud-body-text__small">Chrome v. <span class="cloud-browser-downloads__dl-row-chrome-version">74.0.3729.131</span></span>'

> [version]$xml.span.span.'#text'

Major  Minor  Build  Revision
-----  -----  -----  --------
74     0      3729   131

Upvotes: 4

Related Questions