I am trying to split a string using “.^.” as the delimiter. I have been unable to get it to behave the way I expect it to. For clarification, each segment is separated by “.^.” I don’t want it to split on each character separately. Right now, I’m using something like this.
$str=“yellow.^…^.C.^.5/10/2023 ReissuedSA. CSM approval 3/13/2020 ticket836822. user must run the Authorization Manager application that generates a unique reference code based on the hardware of the machine on which the software has been installed. .^…^.CSM approval 3/13/2020 ticket836822.^.”
$arr=$str.split(“.^.”)
I have tried using $str -split “.^.”, as some posts suggest, but returns a single string without splitting it at all.
I have also read that this may be expecting a RegEx, but I was unable to get that to work either.
If I split with just the caret, it gets closer to the results I’m expecting, but it leaved the periods around each field.
I am working with an existing database also. So I can’t use a different delimiter.
And BTW: Because you formatted your code as quote and not as code the code got messed up - especially your input string.
When you post code, sample data, console output or error messages please format it as code using the preformatted text button ( </> ). Simply place your cursor on an empty line, click the button and paste your code.
( !! Sometimes the preformatted text button hides behind the settings gear symbol. )
… just to show the difference:
$str = "yellow.^..^.C.^.5/10/2023 ReissuedSA. CSM approval 3/13/2020 ticket836822. user must run the Authorization Manager application that generates a unique reference code based on the hardware of the machine on which the software has been installed. .^..^.CSM approval 3/13/2020 ticket836822.^."
$arr = $str -split "\.\^\."
$arr.count
If you run your same command in PS5, it won’t return 7 objects, it’ll return 22.
TLDR: That’s because in PS5, it splits it on each character by default in PowerShell. .NET 4 the string class only had methods that took characters as parameter types IIRC. So PowerShell does its magic on the backend. .NET 5 changed the game a bit, and so PS7 also now behaves differently, specifically you can give it a string to split on. To illustrate it a bit easier:
PS7:
'S.t^r.i.n^g'.Split('.^.')
OUTPUT:
S.t^r.i.n^g
PS5:
'S.t^r.i.n^g'.Split('.^.')
OUTPUT
S
t
r
i
n
g
So PS7, it takes that whole thing and splits it as a string, and since it can’t find any matches, it outputs it the same way.
PS5, it seems the same exact command but because there’s no way to split on a string, it knows its splitting on each individual character, so it does just that.
For this probably. I was simply providing additional context if OP was seeing different results it could be because of PS version with the split method