Regular expression – replace the escape character in PowerShell

I have a string of…

"some text \\computername.example.com\admin$ ".

How do I do the substitution so my final result is just “computername”

My question does not seem to know how to escape the two backslashes. For the sake of simplicity, I would rather not Use regular expressions:)

Edit: It actually looks like stackoverflow also has a problem with double backslashes, it should be a double backslash, not a single backslash

< /div>

First of all, there is absolutely nothing wrong with the proposed regular expression method. However, if you are not interested in it, please check: < p>

$test = "some text \\computername.example.com\admin$"
$test.Split('\')[2]. Split('.')[0]

A very simple test shows that the split speed on my machine is slightly faster than its value:

 12:35:24 |(19)|C:\ PS>Measure-Command {1..10000 | %{'some text \\computername.example.com\admin$'.Split('\')[2] .Split('.')[0]}}


Days: 0
Hours: 0
Minutes: 0
Seconds: 1
Milliseconds: 215
Ticks: 12159984
TotalDays: 1.40740555555556E-05
TotalHours: 0.000337777333333333
TotalMinutes: 0.02026664
TotalSeconds : 1.2159984
TotalMilliseconds: 1215.9984



12:35:34 |(20)|C:\ PS>Measure-Command {1..10000 |% {'Some text \\computername\admin$' -replace'\\\\(\w+)\\(\w+)\$','$1'}}


Days : 0
Hours: 0
Minutes: 0
Seconds: 2
Milliseconds: 335
Ticks: 23351277
TotalDays: 2.70269409722222E-05
TotalHours: 0.000648646583333333
TotalMinutes: 0.038918795
TotalSeconds: 2.3351277
TotalMilliseconds: 2335.1277

I have a string consisting of< p>

"some text \\computername.example.com\admin$".

How do I replace so my final result is just “computername “

My question does not seem to know how to escape the two backslashes. For the sake of simplicity, I would rather not use regular expressions:)

Edit: actually looks like stackoverflow There is also the problem of double backslashes, it should be a double backslash instead of a single backslash

First of all, the regular expression method proposed There is absolutely nothing wrong. But, if you are not interested in it, please check:

$test = "some text \\computername.example.com\admin$" 
$test.Split('\')[2].Split('.')[0]

A very simple test shows that the split speed on my machine is slightly faster than its value:

12:35:24 |(19)|C:\ PS>Measure-Command {1..10000 | %{'some text \\computername.example.com\admin$'.Split('\')[2].Split('.')[0]}}< br />

Days: 0
Hours: 0
Minutes: 0
Seconds: 1
Milliseconds: 215
Ticks: 12159984< br />TotalDays: 1.40740555555556E-05
TotalHours: 0.000337777333333333
TotalMinutes: 0.02026664
TotalSeconds: 1.2159984
TotalMilliseconds: 1215.9984


< br />12:35:34 |(20)|C:\ PS>Measure-Command {1..10000 | %{'Some text \\computername\admin$' -replace'\\\\(\w+) \\(\w+)\$','$1'))


Days: 0
Hours: 0
Minutes: 0
Seconds: 2
Milliseconds: 335
Ticks: 23351277
TotalDays: 2.70269409722222E-05
TotalHours: 0.000648646583333333
TotalMinutes : 0.038918795
TotalSeconds: 2.3351277
TotalMilliseconds: 2335.1277

Leave a Comment

Your email address will not be published.