Home > front end >  powershell - get a single word out of a string
powershell - get a single word out of a string

Time:09-21

I have a string:

<UserInputs><UserInput Question="Groupname" Answer="&lt;Values Count=&quot;1&quot;&gt;&lt;Value DisplayName=&quot;AllHummanresources&quot; Id=&quot;af05c5d3-2312-c897-8439-08979d4d0a49&quot; /&gt;&lt;/Values&gt;" Type="System.SupportingItem.PortalControl.InstancePicker" /><UserInput Question="Ausgabe" Answer="Namen" Type="richtext" /></UserInputs>

I want to trim the string to get as result "AllHummanresources". So I need the word between DisplayName=&quot; and &quot; .

How can I achieve this goal? I did not find a fitting example in the net :(

greetings

CodePudding user response:

You could use the -replace operator so that you omit everything apart from that string.

$string -replace '. (?:DisplayName=&quot;)(.*?)&quot. ', '$1'

Granted this is only as good as the consistency of your input string.

CodePudding user response:

You can use Select-String Cmdlet along with regex.

$result = $your_string |  Select-String -Pattern "DisplayName=&quot;(.*?)&quot;"

If the match is successful you can access the group by

Write-Host $result.Matches.Groups[1].Value
  • Related