0

I try to replace "..\..\..\..\[AnyCharacters]"

to

"..\..\..\..\.."

with regex in powershell.

I tried many patterns, but problem is the same, that it always starts from the begining of string and it changes all instead of last [AnyCharacters]. There could be more backslashes with dots. I do not know how many exactly.

5
  • There is no reason to replace fixed strings with regex. The .NET string replace "..\..\bla".Replace("..\", "") will do just fine. Commented Oct 30, 2019 at 18:22
  • @Tomalak Except if you get a string c:\something\..\autoexec.bat of course. But the meaning is changed for the original path in the question as well. With that in mind, just asking the FS API for an absolute path might be a better idea. Commented Oct 30, 2019 at 18:24
  • 3
    What is the X Y problem? Tell us about X, not Y. Commented Oct 30, 2019 at 18:25
  • @Maarten The OP has not clarified what they actually are after, so all I"m going on is "how to remove dot-dot-backslash from a string". Commented Oct 30, 2019 at 18:40
  • please show us the code you have tried and how it failed. Commented Oct 30, 2019 at 22:05

1 Answer 1

2

i suspect that i have misunderstood your intent. however, if you really want to simply replace the final text with two dots, then this will work. it uses the builtin path handling cmdlets to do the work ... [grin]

$PathString = '..\..\..\..\AnyThingHere.txt'
$DotDot = '..'

Join-Path -Path (Split-Path -Path $PathString -Parent) -ChildPath $DotDot

output = ..\..\..\..\..

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.