powershell: remove text from end of string -


--deleted earlier text - asked wrong question!

ahem....

what have $var = "\\unknowntext1\alwayssame\unknowntext2"

i need keep "\\unknowntext1"

try regular expressions:

$foo = 'something_of_unknown' -replace 'something.*','something' 

or if know partially 'something', e.g.

'something_of_unknown' -replace '(some[^_]*).*','$1' 'some_of_unknown' -replace '(some[^_]*).*','$1' 'somewhatever_of_unknown' -replace '(some[^_]*).*','$1' 

the $1 reference group in parenthesis (the (some[^_]*) part).

edit (after changed question):

if use regex, special characters need escaped:

"\\unknowntext1\alwayssame\unknowntext2" -replace '\\\\unknowntext1.*', '\\unknowntext1' 

or (another regex magic) use lookbehind this:

"\\unknowntext1\alwayssame\unknowntext2" -replace '(?<=\\\\unknowntext1).*', '' 

(which is: take (.*), there must \\unknowntext1 before ('(?<=\\\\unknowntext1)) , replace empty string.

edit (last)

if know there known in middle (the alwayssame), might help:

"\\unknowntext1\alwayssame\unknowntext2" -replace '(.*?)\\alwayssame.*', '$1' 

Comments