runcmd
runcmd

Reputation: 602

Powershell remove text after first instance of special character

This is the value that I have to parse.

8.2.4.151.65; HBAAPI(I) v1.3; 3-29-02

I need to remove everything after and including the first instance of ;

So I need my ending result to be 8.2.4.151.65

Upvotes: 12

Views: 128477

Answers (3)

Rynant
Rynant

Reputation: 24283

Split on the ; and take the first string.

'8.2.4.151.65; HBAAPI(I) v1.3; 3-29-02'.split(';')[0] 

Upvotes: 27

mjolinor
mjolinor

Reputation: 68273

Using a regex with a lazy match:

'8.2.4.151.65; HBAAPI(I) v1.3; 3-29-02' -replace '(.+?);.+','$1'

8.2.4.151.65

The ? makes the match 'lazy', so it stop at the first ;

Upvotes: 5

Doug Finke
Doug Finke

Reputation: 6823

$s = '8.2.4.151.65; HBAAPI(I) v1.3; 3-29-02'
$s.Substring(0, $s.IndexOf(';'))

Upvotes: 28

Related Questions