amatthew382
amatthew382

Reputation: 43

Extracting substring in powershell using regex

I have a string in excel that I need to extract a substring from

This is an example of the string:

<\Text Name="Text5"><TextValue>Hostname:   hostnamehere</TextValue>

I'm new to regex and powershell, but I'm trying to find a way to extract the "hostname here" portion of the string. It's variable length, so indexing won't be reliable.

Upvotes: 0

Views: 402

Answers (2)

js2010
js2010

Reputation: 27428

If that were xml, it would be straightforward

[xml]$xml = '<Text Name="Text5"><TextValue>Hostname:   hostnamehere</TextValue></Text>'
(-split $xml.text.textvalue)[1]

hostnamehere

Upvotes: 0

Lee_Dailey
Lee_Dailey

Reputation: 7479

since you changed the sample, the comment code i posted won't work. [grin] this will, tho ...

$InStuff = '<\Text Name="Text5"><TextValue>Hostname:   hostnamehere</TextValue>'

$InStuff.Split(':')[-1].Split('<')[0].Trim()

output = hostnamehere

if you have a set of sample strings, then you likely otta post them so the code can be arranged to handle the needed variants.

Upvotes: 1

Related Questions