2

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

2

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

@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°).

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.