1

I am attempting to split a text file using the PowerShell -split operator, but unable to figure out the expression.

example

enter image description here

this needs to be split into a total of 4 section e.g.

enter image description here

while the first 2 sections work as expected, the 3rd section can be text with spaces OR text with no spaces OR numbers of a variable length which does not work for me.

$c -split "(\d\d/\d\d/\d\d),\s(\d\d\:\d\d)\s\-\s*"

enter image description here

$c -split "(\d\d/\d\d/\d\d),\s(\d\d\:\d\d)\s\-\s(*\:\s)"

enter image description here

splits it into 3 and unable to split it into 4 as the * seems to override the 4th section

any suggestions how to fix this would be much appreciated

Sachin
  • 65
  • 2
  • 6
  • 2
    Could you use plain text for your _example_ and _expected output_ instead of screenshots? It will be easier for others trying to help you if they can copy what you already have. – Santiago Squarzon Jul 16 '21 at 16:02
  • i did initially but the formatting was messed up so edited and posted an image. will post the example in the comments – Sachin Jul 16 '21 at 16:09
  • 16/01/18, 00:47 - John Doe: Payload_Text 23/01/18, 14:27 - JaneDoe: Payload_Text 16/01/18, 00:47 - Jack the Rabbit: Payload_Text 23/01/18, 14:27 - 12 345 67890: Payload_Text – Sachin Jul 16 '21 at 16:09

1 Answers1

5

Use regex alternation (|) to specify multiple (multi-character) separator strings:

'16/01/18, 00:47 - John Doe: Payload_Text' -split ', | - |: '

Note that the regex you pass to the -split operator specifies what separates the tokens rather than what matches the tokens themselves.

(Enclosing (parts of) the regex in (...), i.e. including capture groups, makes -split include the captured text in the output, but your sample data doesn't require matching any parts of the tokens themselves).

Note that if you have an array of input strings, you'll have to call -split on each in a loop, as direct use of -split will return a single, flat array containing the tokens across all input strings.

mklement0
  • 382,024
  • 64
  • 607
  • 775