使用grep替换bbedit中第一个后的模式的每个实例

时间:2011-08-12 14:56:33

标签: regex formatting

所以我有一个非常长的txt文件,遵循这种模式:

},
"303" :
{
   "id" : "4k4hk2l",
   "color" : "red",
   "moustache" : "no"
},
"303" :
{
   "id" : "4k52k2l",
   "color" : "red",
   "moustache" : "yes"
},
"303" :
{
   "id" : "fask2l",
   "color" : "green",
   "moustache" : "yes"
},
"304" :
{
   "id" : "4k4hf4f4",
   "color" : "red",
   "moustache" : "yes"
},
"304" :
{
   "id" : "tthj2l",
   "color" : "red",
   "moustache" : "yes"
},
"304" :
{
   "id" : "hjsk2l",
   "color" : "green",
   "moustache" : "no"
},
"305" :
{
   "id" : "h6shgfbs",
   "color" : "red",
   "moustache" : "no"
},
"305" :
{
   "id" : "fdh33hk7",
   "color" : "cyan",
   "moustache" : "yes"
},

我正在尝试将其格式化为具有以下结构的正确json对象....

"303" :
   { "list" : [
     {
      "id" : "4k4hk2l",
      "color" : "red",
      "moustache" : "no"
     },
     {
      "id" : "4k52k2l",
      "color" : "red",
      "moustache" : "yes"
     },
     {
      "id" : "fask2l",
      "color" : "green",
      "moustache" : "yes"
     }
    ]
   }
"304" :
   { "list" : [
 etc...

意思是我寻找^“\ d \ d \ d”的所有模式:并保留第一个唯一的模式,但删除所有后续的模式(例如,保留“303”的第一个实例:,但完全删除其余的然后离开“304”的第一个实例:,但完全删除所有其余的等等。)。

我一直在尝试在bbedit应用程序中执行此操作,该应用程序具有搜索/替换的grep选项。我的模式匹配fu太弱,无法完成此任务。有任何想法吗?或者更好的方法来完成这项任务?

1 个答案:

答案 0 :(得分:1)

您无法捕获重复捕获组。捕获将始终仅包含组的最后一个匹配。所以除了在模式中重复你的组之外,你无法用一次搜索/替换来做到这一点。但即使这样,只有在您知道结果组中元素的最大数量时才能成为解决方案。

假设我们有一个简化的数据版本:

1a;1b;1c;1d;1e;2d;2e;2f;2g;3x;3y;3z;

我们看到元素的最大数量是5,所以我们重复捕获组5次。

/([0-9])([a-z]*);?(\1([a-z]);)?(\1([a-z]);)?(\1([a-z]);)?(\1([a-z]);)?/

代替
\1:\2\4\6\8\10; 

然后我们得到了理想的结果:

1:abcde;2:defg;3:xyz;

如果您非常着急(并且在2天后我认为您没有),您可以将此技术应用于您的数据,但使用某种脚本语言将是更好,更清晰的解决方案。

对于我的简化示例,您必须遍历/([0-9])[a-z];?(\1[a-z];?)*/的匹配项。那些将是:

1a;1b;1c;1d;1e;
2d;2e;2f;2g;
3x;3y;3z;

在那里你可以捕获所有值并将它们绑定到响应键,每次迭代只有一个。