Tran Ngu Dang
Tran Ngu Dang

Reputation: 2630

Get certain text in string powershell

I want to get "localhost" (or whatever text) inside this string:

XML-Execute-Result: <host>localhost</host>

I want a general method, like a way to reuse an expression value (like with sed in linux)

sed 's/*[0-9]$/\$&/'

Thank you very much for any reply

Upvotes: 0

Views: 119

Answers (2)

Adil Hindistan
Adil Hindistan

Reputation: 6605

PS> if ($text -match "<host>(.*?)</host>") {$matches[1]}
localhost

Upvotes: 0

David Brabant
David Brabant

Reputation: 43459

$text = "XML-Execute-Result: <host>localhost</host>"
if ($text -match "\<host\>(?<host>.*?)\</host\>")
{
    $myhost = $matches.host
}

Upvotes: 1

Related Questions