I have a string that consists of
"some text \\computername.example.com\admin$".
How would I do a replace so my final result would be just "computername"
My problems appears to not knowing how to escape two backslashes. To keep things simple I would prefer not to use regexp :)
EDIT: Actually looks like stackoverflow is having problems with the double backslash as well, it should be a double backslash, not the single shown
I don't think you're going to get away from regular expressions in this case.
I would use this pattern:
'Some text \\computername\admin$' -replace '\\\\(\w+)\\(\w+)\$', '$1'
which gives you
PS C:\> 'Some text \\computername\admin$' -replace '\\\\(\w+)\\(\w+)\$', '$1'
Some text computername
or if you wanted only the computername from the line:
'Some text \\computername\admin$' -replace '.*\\\\(\w+)\\(\w+)\$', '$1'
which returns
PS C:\> 'Some text \\computername\admin$' -replace '.*\\\\(\w+)\\(\w+)\$', '$1'
computername
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With