我想从
重新格式化我的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.
正则表达式查找并替换编辑:使用Xcode的查找/替换正则表达式引擎将是首选,因为上帝只知道我在200多个不同类的库中有多少doc文档。找到这些块的表达式很简单,但我不知道如何创建替换表达式,以便我能够为每一行添加前缀。
当前搜索表达式: (?s:/\*\*(.*?)\*/)
- 匹配/** */
当前替换表达:/// $1
显然,上面的表达并没有达到我想要的效果。我提前感谢任何帮助!感谢。
答案 0 :(得分:2)
这是我能提出的最好的解决方案,但它依赖于一些专门的PCRE锚点(最重要的是\G
,但我也选择了\A
和\K
)所以它可能不适合你的正则表达式。这也是一个两步解决方案,但我认为不可能把它归结为一个 - 很想看到有人在这里证明我错了!
首先,您希望匹配/**
和*/
之间以空格开头的每一行,并将空格替换为///
。
<强> 查找 强>
~(?<=/[*]{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
上面唯一可能需要额外解释的是我使用的(?<!\A)\G)
“hack”。 \G
让我们在最后一场比赛结束时开始一场比赛,这对我们这里的重复问题非常必要(对于像这样的无所不包的解决方案)。但是,\G
也匹配我们不想要的字符串的开头(我们处理前瞻的前半部分中我们匹配注释开头的那个)所以我们否定匹配字符串的开头与(?<!\A)
。吊杆!
\K
不是必需的,但是当我们把这个复杂化时,它会使表达更清晰。没有它,\n
是匹配的一部分,我们需要手动将其替换为\n\\\ $1
。
答案 1 :(得分:1)
如果您有批次的文件,请编写一个执行以下操作的小脚本(帮您自己帮忙,备份您的数据):
(?s:/\*\*(.*?)\*/)
/**
和*/
部分"/// "
(不带引号)答案 2 :(得分:0)
尽管在Sam's answer中表现得非常出色,但最终还是采用了以下丑陋的暴力方法,万一有人想知道:
第1步
查找:/\*\*\n
替换:""
- 没有
第2步
查找:^\s*-\s*(?=parameter|returns|note|important|precondition|postcondition)
替换:///
第3步
查找:(?<=/// .{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)
替换:""
- 没有