Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replacing escape characters in Powershell

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

like image 668
fenster Avatar asked Jan 23 '23 20:01

fenster


1 Answers

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
like image 110
Steven Murawski Avatar answered Feb 04 '23 02:02

Steven Murawski