Powershell Split wtih multi-character delimiter

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.

Any help is much appreciated.

Works fine for me other than the fact that the “…” keep getting encoded as a single character. Had to replace those with actual “…”

I’m getting over 20 elements in the array. It’s splitting on the periods in the text fields.

Try

$arr = $str  -split "\.\^\."

And BTW: Because you formatted your code as quote and not as code the code got messed up - especially your input string. :point_up:t3: :point_up:t3: :point_up:t3:

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.

Thanks in advance

How to format code in PowerShell.org 1 <---- Click :point_up_2:t4: :wink:

( !! Sometimes the preformatted text button hides behind the settings gear symbol. :wink: )

… 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

The output is “7:smirk:

Thanks. That worked. I tried it with the backslashes before and it didn’t work. I think I used that with the .split method.

Actually

$str.Split('.^.')

works for me as well. The result is the same. :man_shrugging:t3:

One thing I’ll point out using the split method is something that changed between PS5 and 7.

At one point in time I was going to blog but I guess I keep forgetting to do it, but ironically enough, I wrote a blog on this: Using .NET 5 split Method to split strings in PowerShell 7.1+ (dotnvo.github.io)

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.

So it is preferable to stick with the pure PowerShell syntax for your code since you get consistent results in both versions. :man_shrugging:t3: :+1:t3: :love_you_gesture:t3:

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 :slight_smile: