简体   繁体   中英

Including */ in a C-style block comment

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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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