perl嵌套正则表达式

时间:2015-07-13 10:41:57

标签: regex perl perl-module perl-data-structures

文字记录:

(1,2,3,4,{fred,don,max,rat,grp},45,67,mat,jhon,{a,b,1,2,sd[{1,2},{4,5}],45,67,P[{34,56,34},{uni,cast,r}],c{q,ew,3,4},1,2,3,cf{2,4,5,8},6},4,fr{24,45,67},5,NOL)

期望的输出:

(1,2,3,4,{fred:don:max:rat:grp},45,67,mat,jhon,{a:b:1:2:sd[{1:2}:{4:5}]:45:67:P[{34:56:34}:{uni:cast:r}]:c{q:ew:3:4}:1:2:3:cf{2:4:5:8}:6},4,fr{24:45:67},5,NOL)

需要替换为:对于以下内容:

  1. {}[]
  2. 之间的所有内容
  3. 数据具有嵌套结构,例如{{}}{[],[]}
  4. 文字记录始终位于()
  5. 之间

    对perl的任何帮助表示赞赏。

1 个答案:

答案 0 :(得分:-2)

您可以尝试使用以下perl命令,该命令使用正向前导的正则表达式。

perl -pe 's/,(?=(?:\{[^{}]*\}|[^{}])*})|,(?=(?:\[[^\[\]]*\]|[^\[\]])*\])/:/g' file

DEMO

相关问题