gooly
gooly

Reputation: 1341

PowerShell: Split String without removing the split-pattern?

I tried the solution form here but I get the error (my translation) Regex.Split is unknown??
I need to split the line into an string-array but keeping the begin of the lines: "prg=PowerShell°"

my line

    $l = "prg=PowerShell°V=2.0°dtd=20120602°user=kjuz°pwd=jhiuz°chk=876876°prg=PowerShell°V=2.0°dtd=20120602°user=kjuz°pwd=jhiuz°chk=876876°prg=PowerShell°V=2.0°dtd=20120602°user=kjuz°pwd=jhiuz°chk=876876°"
    [string]$x = Regex.Split($l, "(prg=PowerShell°)" )
    $x

I get:

    +         [string]$x = Regex.Split <<<< ($l, "(prg=PowerShell°)" )
            + CategoryInfo          : ObjectNotFound: (Regex.Split:String) [], CommandNotFoundException
            + FullyQualifiedErrorId : CommandNotFoundException

What's wrong?

Upvotes: 2

Views: 5029

Answers (1)

zx81
zx81

Reputation: 41838

Here you go:

$regex = [regex] '(?=prg=PowerShell°)'
$splitarray = $regex.Split($subject);

To split, we are using a zero-width match (i.e., we split without losing characters). To do this, we look ahead to see if the next characters are prg=PowerShell° This is what the regex (?=prg=PowerShell°) does.

Upvotes: 2

Related Questions