0

I'm parsing out a text file using a PowerShell script. Some of the content has the form of:

(1) first thing (2) other thing (that,has,details) (3) third thing: stuff (some details), first thing
(1) first thing (2) other thing (that,has,details) (3) third thing: stuff (some details), first thing (4) potentially (5) more (6) things (7) too

Just like a delimited string, except the delimiter is an incrementing bracketed number. I would like to parse this out into a string array with the contents:

arr[0]="(1) first thing"
arr[1]="(2) other thing (that,has,details)"
arr[2]="(3) third thing: stuff (some details), first thing"

or

arr[0]="first thing"
arr[1]="other thing (that,has,details)"
arr[2]="third thing: stuff (some,details), first thing"

While keeping the solution flexible to handle there being additional fields in the future. It would be extra incredible if I could retain the numbers in a separate array, or have both the numbers and the text in a 2D array.

arr[0,0]="(1)"
arr[0,1]="first thing"
arr[1,0]="(2)"
arr[1,1]="other thing (that,has,details)"
arr[2,0]="(3)"
arr[2,1]="third thing: stuff (some,details), first thing"

I'm trying to get a regular expression going to do this, but having some trouble. Reluctant to hack something together because using a regex would be oh so nice.

Thank you for any help.

Brad_Z
  • 345
  • 2
  • 11

1 Answers1

1
\G(\(\d+\))\s+((?:[^\(]|\((?!\d+\)))*[^\(\s])(?:\s+|$)

https://regex101.com/r/fbvpic/1

Ωmega
  • 37,727
  • 29
  • 115
  • 183