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?
Joey
356k88 gold badges704 silver badges698 bronze badges
asked Jun 3, 2014 at 5:22
1 Answer 1
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.
answered Jun 3, 2014 at 5:28
Sign up to request clarification or add additional context in comments.
1 Comment
Joey
@gooly:
$splitArray = $subject -split '(?=prg=PowerShell°)'
suffices. You don't have to write C# in PowerShell. As a side note, though, you will get an empty element at the start this way (and the other ways we've seen here so far as well). To avoid that you can hack the regex a bit by explicitly not matching at the start of the string: (?<!^)(?=prg=PowerShell°)
.lang-bash