正则表达式替换单行c ++注释

时间:2022-09-13 16:19:14

I would like to insert doxygen comments into my project. I want to do this as automatic as possible, so I had the idea to use regex and the search and replace functionality.
Example:

我想在我的项目中插入doxygen注释。我希望尽可能自动执行此操作,因此我有了使用正则表达式以及搜索和替换功能的想法。例:

int var; //Explanation variable

into:

int var; /**< @brief Explanation variable */

As a bonus, I also don't want to delete my code intend.
I tried different things like

Find: .+;*// .+ (finds the relevant lines)
Replace: .+;*/**< @brief .+ */

Problem is, that it doesn't insert the original text, it inserts the regex as plain text and it deletes the code intend. Is there a way to to this properly? I have read many different posts but I can't get it working.

作为奖励,我也不想删除我想要的代码。我尝试了不同的东西,比如Find:。+; * //。+(找到相关的行)替换:。+; * / ** <@brief。+​​ * /问题是,它没有插入原始文本,它将正则表达式作为纯文本插入,并删除代码打算。有没有办法正确吗?我已经阅读了很多不同的帖子,但我无法让它发挥作用。

3 个解决方案

#1


1  

You need to use capturing groups and backreferences:

您需要使用捕获组和反向引用:

Find what: (.+;\s*)//\s*([^\r\n]+)
Replace with: $1/**< @brief $2 */

找到:(。+; \ s *)// \ s *([^ \ r \ n] +)替换为:$ 1 / ** <@brief $ 2 * /

See the regex demo.

请参阅正则表达式演示。

Details:

  • (.+;\s*) - Group 1 (its value is accessed with the $1 numbered backreference from the replacement pattern): any one or more chars other than line break chars, ; and then 0+ whitespaces
  • (。+; \ s *) - 第1组(使用替换模式中的$ 1编号反向引用访问其值):除了换行符之外的任何一个或多个字符;然后是0+空格

  • // - a double /
  • // - 一个双/

  • \s* - 0+ whitespaces
  • \ s * - 0+空格

  • ([^\r\n]+) - Group 2 (its value is accessed with the $2 numbered backreference from the replacement pattern): any 1+ chars other than newline (\n) and carriage return (\r) chars (as . matches a CR (\r) in .NET regex).
  • ([^ \ r \ n] +) - 第2组(使用替换模式的$ 2编号反向引用访问其值):除换行符(\ n)和回车符(\ r)之外的任何1 +字符(如。匹配.NET正则表达式中的CR(\ r))。

VS 2015 test: 正则表达式替换单行c ++注释

VS 2015测试:

#2


1  

You can try this:

你可以试试这个:

^(\s*[^;]+;\s*)\/\/(.*)$

and replace by this:

并替换为:

$1/**< @brief $2 */

Regex101Demo

#3


0  

I modified the solution of @WiktorStribizew a bit, to make it even more comfortable:

我稍微修改了@WiktorStribizew的解决方案,使其更加舒适:

Find: (.+;[^\r\n]\s*)//\s*([^\r\n]+)

I inserted [^\r\n] into the front, to avoid some special cases like:

我将[^ \ r \ n]插入前面,以避免一些特殊情况,如:

int var;
//Comment for the next function
int function ()

#1


1  

You need to use capturing groups and backreferences:

您需要使用捕获组和反向引用:

Find what: (.+;\s*)//\s*([^\r\n]+)
Replace with: $1/**< @brief $2 */

找到:(。+; \ s *)// \ s *([^ \ r \ n] +)替换为:$ 1 / ** <@brief $ 2 * /

See the regex demo.

请参阅正则表达式演示。

Details:

  • (.+;\s*) - Group 1 (its value is accessed with the $1 numbered backreference from the replacement pattern): any one or more chars other than line break chars, ; and then 0+ whitespaces
  • (。+; \ s *) - 第1组(使用替换模式中的$ 1编号反向引用访问其值):除了换行符之外的任何一个或多个字符;然后是0+空格

  • // - a double /
  • // - 一个双/

  • \s* - 0+ whitespaces
  • \ s * - 0+空格

  • ([^\r\n]+) - Group 2 (its value is accessed with the $2 numbered backreference from the replacement pattern): any 1+ chars other than newline (\n) and carriage return (\r) chars (as . matches a CR (\r) in .NET regex).
  • ([^ \ r \ n] +) - 第2组(使用替换模式的$ 2编号反向引用访问其值):除换行符(\ n)和回车符(\ r)之外的任何1 +字符(如。匹配.NET正则表达式中的CR(\ r))。

VS 2015 test: 正则表达式替换单行c ++注释

VS 2015测试:

#2


1  

You can try this:

你可以试试这个:

^(\s*[^;]+;\s*)\/\/(.*)$

and replace by this:

并替换为:

$1/**< @brief $2 */

Regex101Demo

#3


0  

I modified the solution of @WiktorStribizew a bit, to make it even more comfortable:

我稍微修改了@WiktorStribizew的解决方案,使其更加舒适:

Find: (.+;[^\r\n]\s*)//\s*([^\r\n]+)

I inserted [^\r\n] into the front, to avoid some special cases like:

我将[^ \ r \ n]插入前面,以避免一些特殊情况,如:

int var;
//Comment for the next function
int function ()