7

--deleted earlier text - I asked the wrong question!

ahem....

What I have is $var = "\\unknowntext1\alwaysSame\unknowntext2"

I need to keep only "\\unknowntext1"

0

3 Answers 3

9

Try regular expressions:

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

Or if you know only partially the 'something', then e.g.

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

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

Edit (after changed question):

If you use regex, then special characters need to be escaped:

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

or (another regex magic) use lookbehind like this:

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

(which is: take anything (.*), but there must be \\unknowntext1 before it ('(?<=\\\\unknowntext1)) and replace it with empty string.

Edit (last)

If you know that there is something known in the middle (the alwaysSame), this might help:

"\\unknowntext1\alwaysSame\unknowntext2" -replace '(.*?)\\alwaysSame.*', '$1'
Sign up to request clarification or add additional context in comments.

1 Comment

Edited the answer, but as I see now, I have to edit it once again ;)
6

What I have is $var = "\unknowntext1\alwaysSame\unknowntext2"

I need to keep only "\unknowntext1"

Not sure this requires a regular expression. Assuming alwaysSame is literally always the same, as the discussion around stej's answer suggests, it seems by far the most straightforward way to accomplish this would be:

$var.substring(0, $var.indexOf("\alwaysSame"));

2 Comments

How does that not answer the question? If you have $var = "\\unknowntext1\alwaysSame\unknowntext2" and you run $var.substring(0, $var.indexOf("\alwaysSame"));, you get \\unknowntext1, QED. Was just surprised the other answers were nontrivially more convoluted.
I notice neither of you (Hashem & Lucas) have a powershell tag in your profile. Lines of Powershell code always* emit their results to the "pipeline", if that's throwing you. That statement in my answer will "return" the value the OP requested; every PowerShell line of code is sort of like an implicit function which can be chained to another subsequent command. I can see how that might have confused you. I've also edited the answer to make my intent clearer to non-PS users.
5
function Remove-TextAfter {   
    param (
        [Parameter(Mandatory=$true)]
        $string, 
        [Parameter(Mandatory=$true)]
        $value,
        [Switch]$Insensitive
    )

    $comparison = [System.StringComparison]"Ordinal"
    if($Insensitive) {
        $comparison = [System.StringComparison]"OrdinalIgnoreCase"
    }

    $position = $string.IndexOf($value, $comparison)

    if($position -ge 0) {
        $string.Substring(0, $position + $value.Length)
    }
}

Remove-TextAfter "something_of_unknown" "SoMeThInG" -Insensitive

1 Comment

Thanks for the System.StringComparison format. ;^)

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.