简体   繁体   English

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

[英]Including */ in a C-style block comment

Is there any way to include */ in a C-style block comment? 有没有办法在C风格的块注释中包含* /? Changing the block comment to a series of line comments (//) is not an option in this case. 在这种情况下,将块注释更改为一系列行注释(//)不是一个选项。

Here's an example of the sort of comment causing a problem: 以下是导致问题的评论类型的示例:

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

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

Nope! 不! There isn't. 没有。

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

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. 当然,正如上面片段中的时髦色彩所证明的那样,你的其他工具可能不知道你在做什么,并且会做出不正确的猜测。

For this specific case, you can change the delimiter on your perl regex. 对于这种特定情况,您可以更改 perl正则表达式上的分隔符 You can use any non-alphanumeric, non-whitespace delimiter. 您可以使用任何非字母数字,非空白分隔符。 Here I switched to # : 我转到#

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM