YEMyslf
YEMyslf

Reputation: 447

Selecting strings in between two sets of characters in PowerShell

I need to select a string between two sets of characters in PowerShell and could use some help. I have some csv data and one of the columns contains a string that I need to select a portion of to populate two other columns. I've got one of these done but need help with the other This is what my text looks like:

My-Computer-IneedthisABC1234567
YourComputer-IneedthistooABC9876543

Basically I need the numerical portion and the ABC portion will always be the same so I'm retrieving with the number with the following:

($Computer.nickname -split "ABC")[0] 

I'm stuck on how to retrieve the Ineedthisand Ineedthistoo part. It will always come in between - and ABC

I suspect a Regex is the way to go with this but I'm just not sure where to start with that.

Upvotes: 1

Views: 4864

Answers (1)

4c74356b41
4c74356b41

Reputation: 72191

Well, a really hackish way would be:

$var = ($Computer.nickname -split "ABC")[0] 
$var.split('-')[-1]

or simply:

(($Computer.nickname -split "ABC")[0]).split('-')[-1]

that would split on - and get last element

Upvotes: 3

Related Questions