[string] Cut off text in string after/before separator in powershell

Ok so here is what I'm trying to do: I want to make a little powershell script that takes each file of my music library and then does a hash sum of it and writes that into a file like so:

test.txt ; 131 136 80 89 119 17 60 123 210 121 188 42 136 200 131 198

Now when I start the script I need it to first compare my music library with the already existing values but for this I just want to cut off everything after the ; so that it can compare filename against filename (or filepath)... but I'm stumped at how to do that, tried replacing with

$name = $name -replace ";*",""

no go...

tried to filter... don't know how D:

I'd really appreciate help.

Also if you think I'm using the wrong coding language, tell me and what would be better, it's just I only ever used C and powershell

This question is related to string powershell

The answer is


Using regex, the result is in $matches[1]:

$str = "test.txt ; 131 136 80 89 119 17 60 123 210 121 188 42 136 200 131 198"
$str -match "^(.*?)\s\;"
$matches[1]
test.txt

This does work for a specific delimiter for a specific amount of characters between the delimiter. I had many issues attempting to use this in a for each loop where the position changed but the delimiter was the same. For example I was using the backslash as the delimiter and wanted to only use everything to the right of the backslash. The issue was that once the position was defined (71 characters from the beginning) it would use $pos as 71 every time regardless of where the delimiter actually was in the script. I found another method of using a delimiter and .split to break things up then used the split variable to call the sections For instance the first section was $variable[0] and the second section was $variable[1].


$name -replace ";*",""

You were close, but you used the syntax of a wildcard expresson rather than a regular expression, which is what the -replace operator expects.

Therefore (hash sequence shortened):

PS> 'test.txt ; 131 136 80 89 119 17 60 123 210 121 188' -replace '\s*;.*'
test.txt

Note:

  • Omitting the substitution-text operand (the 2nd RHS operand) implicitly uses "" (the empty string), i.e. it effectively removes what the regex matched.

  • .* is what represents a potentially empty run (*) of characters (.) in a regex - it is the regex equivalent of * by itself in a wildcard expression.

  • Adding \s* before the ; in the regex also removes trailing whitespace (\s) after the filename.

  • I've used '...' rather than "..." to enclose the regex, so as to prevent confusion between what PowerShell expands up front (see expandable strings in PowerShell and what the .NET regex engine sees.


I had a dir full of files including some that were named invoice no-product no.pdf and wanted to sort these by product no, so...

get-childitem *.pdf | sort-object -property @{expression={$\_.name.substring($\_.name.indexof("-")+1)}}

Note that in the absence of a - this sorts by $_.name


$text = "test.txt ; 131 136 80 89 119 17 60 123 210 121 188 42 136 200 131 198"

$text.split(';')[1].split(' ')

You can use a Split :

$text = "test.txt ; 131 136 80 89 119 17 60 123 210 121 188 42 136 200 131 198"
$separator = ";" # you can put many separator like this "; : ,"

$parts = $text.split($separator)

echo $parts[0] # return test.txt
echo $parts[1] # return the part after the separator