0

Folks

I am unable to replace the below string in PowerShell, initially I suspected it to be due to regexp and used [regex]::Escape() to resolve the \ to double slash however it still does not work. Can you please suggest?

PS C:\User>"C:clog" -replace "C:c" , ""
PS C:\User>log
PS C:\User>"C:\\c\\log" -replace "C:\\c\\", ""
PS C:\User>C:\\c\\log
sodawillow
  • 12,497
  • 4
  • 34
  • 44
Sudheej
  • 1,873
  • 6
  • 30
  • 57
  • 1
    You defined 2 backslashes in the input string, so you need 4 in the pattern: `"C:\\c\\log" -replace "C:\\\\c\\\\", ""` – Wiktor Stribiżew Feb 13 '17 at 22:43
  • Wiktor you are right i achieved this now with another [regex]::Escape() over the input string. Please post in answer. – Sudheej Feb 14 '17 at 04:18

1 Answers1

0

Use Split-Path, it is designed for this:

Split-Path "C:\\c\\log" -Leaf

# returns "log"
sodawillow
  • 12,497
  • 4
  • 34
  • 44