正则表达式按字母顺序将单词插入文本

regex insert a word into a text in alphabetical order

我已经实现了一个 ruby 脚本,它应该生成默认代码段并将其插入到正确的位置。问题是其中一些代码片段必须遵守现有代码的字母顺序。

例如,假设我们需要将单词 'thumb' 的代码片段(在本例中为 UPDATEFIELD((&(thumb)), ([custominputstream readString])); )插入到以下代码:

    //...
    UPDATEFIELD((&(answers)), ((ListTO *)[custominputstream readCustomSerializable]));
    UPDATEFIELD((&(text)), ([custominputstream readString]));
    UPDATEFIELD((&(title)), ([custominputstream readString]));
    //...

希望的结果是:

    //...
    UPDATEFIELD((&(answers)), ((ListTO *)[custominputstream readCustomSerializable]));
    UPDATEFIELD((&(text)), ([custominputstream readString]));
    UPDATEFIELD((&(thumb)), ([custominputstream readString]));  //here is the right place to do it.
    UPDATEFIELD((&(title)), ([custominputstream readString]));
    //...

问题是如何使用正则表达式找到这个精确的文本插入位置/是否可能?

您当前的词序是

answers < text < title

解决该问题的一种方法是从顶部读取文件并在中心断开文件。

你的情况是

UPDATEFIELD((&(text)), ([custominputstream readString]));

阅读

/(?<=UPDATEFIELD\(\(&\()\w+(?=\))/ 字符串 这只不过是 text

然后将其与您的单词进行比较。在你的情况下恰好是 thumb

if 'thumb' < 'text'
  #place it above text
else #(which is obvious) then 
  #place it below
end

顺序变成

answers < text < thumb < title

您的文件很大,因此您可能需要重复相同的练习,直到达到您的目标。

我们正在binary search到达我们需要插入单词的地方。