2

I've got an issue and I'm sure it's something simple I'm overlooking or not understanding when using regex and the alternation method for expression matching for double and single white space.

I'm using the shorthand meta characters \s|\s\s with PowerShell -split to return multiple strings objects with each on a new line per each single or double white space so everything else is on its own line splitting as expected.

Example Data and PowerShell Command

Note: Unfortunately this is an example of the data I'm working with and I have no control over that so it'll have the single and double white space both

$Content = "Data is over here
and here is some down  under too"

$Content -split "\s|\s\s"

Result

Data
is
over
here

and
here
is
some
down

under
too

Expected Result

Data
is
over
here
and
here
is
some
down
under
too

Environment Specs

  • Windows 10 Pro X64
  • PowerShell 5.0

Question

I'd like to understand what's going on with the regex format I'm using with the shorthand meta character alternation syntax but I'll consider a workaround as well if I get nothing definitive otherwise.

  • The first \s will always match first. So on double spaces you're splitting two times, once for each space. Also, it's not adding new lines to the string, but rather returning multiple string objects. – jscott Apr 14 '17 at 11:43
  • Luke's answer is the way I would do it. It's not a "work around". I'm not clear how you can say "you understand the \s will match first" yet not see the \s\s will never be checked as there was already a match. – jscott Apr 14 '17 at 15:45
  • 1
    -split '\s\s|\s' – PetSerAl Apr 14 '17 at 16:10
  • @jscott I added an answer with the content that I was really looking for with my question. I understood what you were saying, but I was just looking for the specifics, etc. with the method of regex I was using and a way to get it to work that way; a way to use regex alternation and the shorthand form of regex for the white spaces. You'll see PetSerAl gave me the needed detail and from there was I able to figure out what I was missing with more detail. I apologize if there's something else I overlooked you mentioned though - thanks for your time. – Pimp Juice IT Apr 15 '17 at 18:01
3

Use this instead, which means one or more occurrences of any white space character, such as tabs, spaces, and so forth:

$Content -split "\s{1,}"

Result:

PS C:\WINDOWS\system32> $Content = "Data is over here
and here is some down  under too"

$Content -split "\s{1,}"
Data
is
over
here
and
here
is
some
down
under
too

PS C:\WINDOWS\system32> 
2

Big thanks to PetSerAl and as pointed out with his comment: "-split '\s\s|\s'PetSerAl"

The Gotcha

I guess there's an important lesson to learn when using regex alternations and that is the order in which you put them could matter greatly.

The Reason

The reason this didn't work was because the expression was looking for a single white space character first and this was a problem because every time it ran into a place where there were two white spaces, one was left over. So this is why I saw a few blank lines in the output.


The Solution

I fixed this by simply switching the positions inside the alternation and told it to favor double white space over single white space (i.e. used \s\s|\s and not \s|\s\s) so it splits on two white spaces first, and if there isn't a double white space, it'll only then split on a single white space.

Solution Script

$Content = "Data is over here
and here is some down  under too"
$Content

$Content -split "\s\s|\s"

Result

Data
is
over
here
and
here
is
some
down
under
too

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.