gorrch
gorrch

Reputation: 551

powershell replace with regex any char except '.'

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.

Upvotes: 0

Views: 226

Answers (1)

Lee_Dailey
Lee_Dailey

Reputation: 7489

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 = ..\..\..\..\..

Upvotes: 2

Related Questions