包括* /在C风格的块注释中

时间:2022-10-01 22:24:04

Is there any way to include */ in a C-style block comment? Changing the block comment to a series of line comments (//) is not an option in this case.

有没有办法在C风格的块注释中包含* /?在这种情况下,将块注释更改为一系列行注释(//)不是一个选项。

Here's an example of the sort of comment causing a problem:

以下是导致问题的评论类型的示例:

/**
 * perl -pe 's/(?<=.{6}).*//g' : Limit to PID
 */

5 个解决方案

#1


24  

Usually comments don't need to be literal, so this doesn't come up too often.

通常评论不需要是文字,所以这不会经常出现。

You can wrap it all in a #if block:

你可以将它全部包装在#if块中:

#if 0
whatever you want can go here, comments or not
#endif

#2


10  

Nope! There isn't.

不!没有。

#3


3  

You can side-step the issue by munging your regex to not include the offending sequence of characters. From the looks of what you're doing, this should should work (make the * non-greedy):

您可以通过将正则表达式设置为不包含有问题的字符序列来解决问题。从你正在做的事情看,这应该工作(使*非贪婪):

/**
 * perl -pe 's/(?<=.{6}).*?//g' : Limit to PID
 */

#4


2  

In the general case, you can't.

在一般情况下,你不能。

Here's a tricky answer that happens to work in this case:

在这种情况下,这是一个棘手的答案:

/**
 * perl -pe 's/(?<=.{6}).* //gx' : Limit to PID
 */

This is (or should be, I didn't actually test the perl command) a regex that matches the same as the original because the x modifier allows whitespace to be used for clarity in the expression, which allows the * to be separated from the /.

这是(或者应该是,我实际上没有测试perl命令)与原始匹配的正则表达式,因为x修饰符允许在表达式中使用空格来清晰,这允许*与/。

You could use more whitespace, I've included just the single space that breaks the end of comment block token.

你可以使用更多的空格,我只包含了打破注释块标记结束的单个空格。

Some compilers support an option to turn on the non-standard feature of allowing nested comments. This is usually a bad idea, but in this particular case you could also do

一些编译器支持打开允许嵌套注释的非标准功能的选项。这通常是一个坏主意,但在这种特殊情况下你也可以这样做

/** 
 * /* perl -pe 's/(?<=.{6}).*//g' : Limit to PID
 */

with that option turned on for just this source file. Of course as demonstrated by the funky coloring in the above fragment, the rest of your tools may not know what you are up to and will make incorrect guesses.

只为此源文件打开了该选项。当然,正如上面片段中的时髦色彩所证明的那样,你的其他工具可能不知道你在做什么,并且会做出不正确的猜测。

#5


0  

For this specific case, you can change the delimiter on your perl regex. You can use any non-alphanumeric, non-whitespace delimiter. Here I switched to #:

对于这种特定情况,您可以更改perl正则表达式上的分隔符。您可以使用任何非字母数字,非空白分隔符。我转到#:

/** 
 * perl -pe 's#(?<=.{6}).*##g' : Limit to PID
 */

Common choices are # and %.

常见的选择是#和%。

'Bracketing characters' like parens or braces get a slightly different syntax, because they are expected to be matched pairs:

“包围字符”(例如parens或braces)的语法略有不同,因为它们应该是匹配的对:

/** 
 * perl -pe 's[(?<=.{6}).*][]g' : Limit to PID
 */

#1


24  

Usually comments don't need to be literal, so this doesn't come up too often.

通常评论不需要是文字,所以这不会经常出现。

You can wrap it all in a #if block:

你可以将它全部包装在#if块中:

#if 0
whatever you want can go here, comments or not
#endif

#2


10  

Nope! There isn't.

不!没有。

#3


3  

You can side-step the issue by munging your regex to not include the offending sequence of characters. From the looks of what you're doing, this should should work (make the * non-greedy):

您可以通过将正则表达式设置为不包含有问题的字符序列来解决问题。从你正在做的事情看,这应该工作(使*非贪婪):

/**
 * perl -pe 's/(?<=.{6}).*?//g' : Limit to PID
 */

#4


2  

In the general case, you can't.

在一般情况下,你不能。

Here's a tricky answer that happens to work in this case:

在这种情况下,这是一个棘手的答案:

/**
 * perl -pe 's/(?<=.{6}).* //gx' : Limit to PID
 */

This is (or should be, I didn't actually test the perl command) a regex that matches the same as the original because the x modifier allows whitespace to be used for clarity in the expression, which allows the * to be separated from the /.

这是(或者应该是,我实际上没有测试perl命令)与原始匹配的正则表达式,因为x修饰符允许在表达式中使用空格来清晰,这允许*与/。

You could use more whitespace, I've included just the single space that breaks the end of comment block token.

你可以使用更多的空格,我只包含了打破注释块标记结束的单个空格。

Some compilers support an option to turn on the non-standard feature of allowing nested comments. This is usually a bad idea, but in this particular case you could also do

一些编译器支持打开允许嵌套注释的非标准功能的选项。这通常是一个坏主意,但在这种特殊情况下你也可以这样做

/** 
 * /* perl -pe 's/(?<=.{6}).*//g' : Limit to PID
 */

with that option turned on for just this source file. Of course as demonstrated by the funky coloring in the above fragment, the rest of your tools may not know what you are up to and will make incorrect guesses.

只为此源文件打开了该选项。当然,正如上面片段中的时髦色彩所证明的那样,你的其他工具可能不知道你在做什么,并且会做出不正确的猜测。

#5


0  

For this specific case, you can change the delimiter on your perl regex. You can use any non-alphanumeric, non-whitespace delimiter. Here I switched to #:

对于这种特定情况,您可以更改perl正则表达式上的分隔符。您可以使用任何非字母数字,非空白分隔符。我转到#:

/** 
 * perl -pe 's#(?<=.{6}).*##g' : Limit to PID
 */

Common choices are # and %.

常见的选择是#和%。

'Bracketing characters' like parens or braces get a slightly different syntax, because they are expected to be matched pairs:

“包围字符”(例如parens或braces)的语法略有不同,因为它们应该是匹配的对:

/** 
 * perl -pe 's[(?<=.{6}).*][]g' : Limit to PID
 */