user2747211
user2747211

Reputation:

How to get variable between two charaters in PowerShell?

I have a PowerShell script that gets part of a string between >and <:

$splitArray = "SomeText>MyVariable<SomeMoreText" -split ">",0
$string = $splitArray[1] -split "<",0
$MyVariable = $string[0]

Is there a more efferent way of getting the MyVariable between the > <?

Upvotes: 1

Views: 107

Answers (1)

EBGreen
EBGreen

Reputation: 37790

You could use a regex replace:

$myVar = "SomeText>MyVariable<SomeMoreText" -replace '.+>(.+)<.+', '$1'

Upvotes: 3

Related Questions