使用正则表达式解析带编号分隔的字符串



我正在使用PowerShell脚本解析一个文本文件。部分内容的形式为:

(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

就像带分隔符的字符串一样,除了分隔符是一个带括号的递增数字。我想把它解析成一个字符串数组,其中包含以下内容:

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

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

同时保持解决方案的灵活性,以应对未来的其他领域。如果我能把数字保留在一个单独的数组中,或者把数字和文本都放在2D数组中,那将是非常不可思议的。

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"

我正试图让正则表达式执行此操作,但遇到了一些问题。不愿意一起破解一些东西,因为使用正则表达式会非常好。

谢谢你的帮助。

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

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

相关内容

最新更新