Regex 正则表达式向匹配中的每一行添加前缀

Regex 正则表达式向匹配中的每一行添加前缀,regex,swift,string,replace,Regex,Swift,String,Replace,我想将我的Swift文档从 /** Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to ma

我想将我的Swift文档从

/**
  Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
  Lorem Ipsum has been the industry's standard dummy text ever since the 
  1500s, when an unknown printer took a galley of type and scrambled it to 
  make a type specimen book. It has survived not only five centuries, but 
  also the leap into electronic typesetting, remaining essentially 
  unchanged. It was popularised in the 1960s with the release of Letraset 
  sheets containing Lorem Ipsum passages, and more recently with desktop 
  publishing software like Aldus PageMaker including versions of Lorem Ipsum.
  */

最好使用Xcode的find/replace regex引擎使用正则表达式find and replace edit:,因为上帝只知道我的200多个不同类的库中有多少文档注释。查找这些块的表达式很简单,但我不知道如何生成替换表达式,以便能够为每行添加前缀

当前搜索表达式:
(?s:/\*\*(.*?\*/)
-匹配
/***/
之间的所有文本

当前替换表达式
//$1

显然,上面的表达并没有达到我想要的效果。我感谢任何帮助,提前!谢谢。

如果您有很多文件,请编写一个小脚本,执行以下操作(帮自己一个忙,备份数据)

  • 查找带有正则表达式的注释
    (?s://\*\*(.*?\*/)
  • 移除
    /**
    */
    零件
  • 将缩进替换为
    “//”
    (不带引号)
  • 用新样式放回注释

  • 这是我能想到的最好的解决方案,但它依赖于一些专门的PCRE锚(最重要的是
    \G
    ,但我也使用了
    \A
    \K
    ),因此它可能无法在您喜欢的正则表达式中工作。这也是一个两步解决方案,但我认为不可能把它归结为一步——希望有人在这里证明我错了


    首先,要匹配
    /**
    */
    之间以空格开头的每一行,并用
    //
    替换空格

    查找:


    然后,我们要获取替换的结果,并删除表示旧注释的剩余行,
    /**
    */

    查找:

    替换:


    上面的大部分内容都很简单,或者至少应该假设您对regex有基本的了解……看起来您确实了解。最明显的是第一个。让我们把它分解(耶!)

    (?
    上面唯一需要额外解释的是
    (?“hack”我使用。
    \G
    允许我们在最后一次匹配结束时开始匹配,这是非常必要的(对于这样一个包罗万象的解决方案)但是,
    \G
    也匹配我们不想要的字符串的开头(我们在前瞻的前半部分处理它,在那里我们匹配注释的开头),所以我们否定了将字符串的开头与
    匹配(?.Boom


    \K
    不是必需的,但当我们变得复杂时,它会使表达式更清晰。没有它,
    \n
    是匹配的一部分,我们需要手动将其替换为
    \n\\\\\$1

    尽管我在中进行了出色的表达式工作,但最终还是采用了以下丑陋的暴力方法,以防有人想知道:

    步骤1

    查找:
    /\*\*\n

    替换:
    -无

    步骤2

    查找:
    ^\s*-\s*(?=参数|返回|注意|重要|前提|后条件)

    替换:
    /

    步骤3
    查找:
    (?关键问题:您使用什么工具来进行替换?Xcode的查找和替换正则表达式引擎。@TimBiegeleisenI不确定您是否可以用一个查找和替换来完成…@ThomasAyoub很遗憾,我也这么认为;有什么建议如何使用多个步骤进行替换吗?不容易,但我明白了!还有两个步骤(我坚定地认为这是不可能的),并且还需要PCRE正则表达式,我不确定Xcode是否适合。第一个解释(疯狂)表达式输入。使用脚本思考得很好。@NoodleofDeath谢谢。当你喜欢写正则表达式时,你就喜欢写正则表达式。我在更新的版本中扩展/解释了主要表达式。请随意提问…希望它在Xcode中工作,lol。相信我,我知道它是如何工作的。我在正则表达式中写得越多,我就越着迷。谢谢你的详细说明解释有问题。希望我能不止一次投票。是的,我在工作中需要经常使用它们,所以我经常“练习”它们。你总是可以奖励赏金;)但是,我满足于+25和其他慢慢进入的东西。享受吧!
    
    /// Lorem Ipsum is simply dummy text of the printing and typesetting industry. 
    /// Lorem Ipsum has been the industry's standard dummy text ever since the 
    /// 1500s, when an unknown printer took a galley of type and scrambled it to 
    /// make a type specimen book. It has survived not only five centuries, but 
    /// also the leap into electronic typesetting, remaining essentially 
    /// unchanged. It was popularised in the 1960s with the release of Letraset 
    /// sheets containing Lorem Ipsum passages, and more recently with desktop 
    /// publishing software like Aldus PageMaker including versions of Lorem Ipsum.
    
    ~(?<=/[*]{2}|(?<!\A)\G)\n\K^\s*+(?![*]/)(.*)$~gm
    
    /// $1
    
    ~^.*(?:/[*]{2}|[*]/).*$\n?~gm
    
    [null]
    
    (?<=       # start a zero-length lookahead
      /[*]{2}  # look for the start of a comment
     |         # or...
      (?<!\A)  # negate this part if we're at the beginning of the string
      \G       # start at the end of the last match (or beginning of string)
    )          # end that lookahead and move on to each line
    \n\K       # find the newline and then reset the match for clarity
    ^\s*+      # match whitespace at the beginning of the line
    (?![*]/)   # negate this match if we're at the end of the comment
    (.*)$      # capture everything up until the end of the line