【问题标题】:Regular expression to add a prefix to each line in a match正则表达式为匹配中的每一行添加前缀
【发布时间】:2016-09-08 15:48:36
【问题描述】:

我想从

重新格式化我的 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.
  */

/// 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.

正则表达式查找和替换 edit: ,使用 Xcode 的查找/替换正则表达式引擎, 将是首选,因为上帝只知道我的 200 多个不同类的库中有多少文档注释。查找这些块的表达式很简单,但我不知道如何制作我的替换表达式,以便我能够为每一行添加一个前缀。

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

当前替换表达式/// $1

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

【问题讨论】:

  • 关键问题:您使用什么工具进行替换?
  • Xcode 的查找和替换正则表达式引擎。 @TimBiegeleisen
  • 我不确定您是否可以通过一次查找和替换来完成...
  • @ThomasAyoub 不幸的是,我想这么多;有什么建议如何使用不止一个步骤来完成吗?
  • 不容易,但我明白了!仍然有两个步骤(我坚定地认为这是不可能的),并且还需要 PCRE 正则表达式,我不确定 Xcode 是否适合。第一个(疯狂的)表达式传入的解释。

标签: regex swift string replace


【解决方案1】:

这是我能想到的最好的解决方案,但它依赖于一些专门的 PCRE 锚(最重要的是 \G,但我也选择了 \A\K)所以它可能不适合你的口味的正则表达式。这也是一个两步解决方案,但我认为不可能将其归结为一个 - 希望看到有人在这里证明我错了!


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

查找:

~(?<=/[*]{2}|(?<!\A)\G)\n\K^\s*+(?![*]/)(.*)$~gm

替换:

/// $1

Demo.


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

查找:

~^.*(?:/[*]{2}|[*]/).*$\n?~gm

替换:

[null]

Demo.


以上大部分内容都很简单,或者至少应该假设您对正则表达式有基本的了解......看起来好像您做到了。最突出的是第一个。让我们分解一下(耶!)...

(?<=       # 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

上面唯一需要额外解释的是我使用的(?&lt;!\A)\G)“hack”。 \G 让我们在最后一场比赛结束时开始一场比赛,这在我们这里遇到的重复问题中是非常必要的(对于像这样的包罗万象的解决方案)。然而,\G 也匹配了我们不想要的字符串的开头(我们在前半部分处理我们匹配注释开头的字符串)所以我们否定匹配字符串的开头 @ 987654339@。轰隆隆!

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

【讨论】:

  • @NoodleofDeath 谢谢。当您喜欢编写正则表达式时,您就喜欢编写正则表达式。我在更新中扩展/解释了主要表达式。随意提问...希望它在 Xcode 中有效,哈哈。
  • 相信我,我知道它是怎么回事。我用正则表达式写的越多,我就越着迷。感谢您的详细解释。希望我能不止一次投票。
  • 是的,我在工作中经常需要使用它们,所以我经常在 SO 上“练习”。你总是可以奖励赏金;)但是,我对 +25 和其他慢慢流入的东西很满意。享受吧!
【解决方案2】:

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

  1. 使用正则表达式 (?s:/\*\*(.*?)\*/) 查找 cmets
  2. 删除 /***/ 部分
  3. 将缩进替换为"/// "(不带引号)
  4. 以新样式放回 cmets。

【讨论】:

  • 使用脚本的好想法。
【解决方案3】:

尽管Sam's answer 中的表达工作非常出色,但我最终还是做了以下丑陋的蛮力方法,以防有人想知道:

第 1 步

查找:/\*\*\n
替换:"" - 没有

第 2 步

查找:^\s*-\s*(?=parameter|returns|note|important|precondition|postcondition)
替换:///

第 3 步 查找:(?&lt;=/// .{0,80}?\n)(\s*)(?!(\s*(case|let|convenience|enum|struct|init|required|override|weak|public|private|static|class|var|func|lazy|@|///))) 替换:$1///

第 4 步 查找:^\s*\*/\n(?=case|let|convenience|enum|struct|init|required|override|weak|public|private|static|class|var|func|lazy)
替换:"" - 没有

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-26
    相关资源
    最近更新 更多