【问题标题】:parsing numbered delimited string using regular expression使用正则表达式解析带编号的分隔字符串
【发布时间】:2020-08-03 11:45:09
【问题描述】:

我正在使用 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"

同时保持解决方案的灵活性以处理未来的其他字段。如果我可以将数字保留在一个单独的数组中,或者将数字和文本都保存在一个二维数组中,那就太不可思议了。

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"

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

感谢您的帮助。

【问题讨论】:

    标签: regex powershell parsing text-parsing csv


    【解决方案1】:
    \G(\(\d+\))\s+((?:[^\(]|\((?!\d+\)))*[^\(\s])(?:\s+|$)
    

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

    【讨论】:

      猜你喜欢
      • 2016-12-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-04
      相关资源
      最近更新 更多