Bonjour,
Je souhaiterais détecter les commentaires multilignes (/* ... */) contenus dans des fichiers C++: .h, .cpp.
Pour ce faire, j'utilise boost::regex. J'ai déjà trouvé une expression rationnelle (ou régulière comme vous souhaitez ) qui détecte les commentaires contenus sur une seule ligne.
"/\\*([[:punct:]]|[[:alnum:]]|[[:blank:]])*\\*/"
Mais pas moyen de lui faire reconnaître les multilignes, j'ai bien essayé de remplacer [:blank:] par [:space:], voir même :
mais le problème c'est qu'il ne s'arrête pas au premier */ mais au dernier.
Voici l'exemple simple sur lequel je travaille:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| /* Check to see if a file is a PNG file using png_sig_cmp(). png_sig_cmp()
* returns zero if the image is a PNG and nonzero if it isn't a PNG.
*
* The function check_if_png() shown here, but not used, returns nonzero (true)
* if the file can be opened and is a PNG, 0 (false) otherwise.
*
* ...
*/
#define PNG_BYTES_TO_CHECK 4
int check_if_png(char *file_name, FILE **fp)
{
char buf[PNG_BYTES_TO_CHECK];
/* Open the prospective PNG file. */
if ((*fp = fopen(file_name, "rb")) == NULL)
return 0;
/* Read in some of the signature bytes */
if (fread(buf, 1, PNG_BYTES_TO_CHECK, *fp) != PNG_BYTES_TO_CHECK)
return 0;
/* Compare the first PNG_BYTES_TO_CHECK bytes of the signature.
Return nonzero (true) if they match */
return(!png_sig_cmp(buf, (png_size_t)0, PNG_BYTES_TO_CHECK));
} |
Le résultat est donc:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| /* Check to see if a file is a PNG file using png_sig_cmp(). png_sig_cmp()
* returns zero if the image is a PNG and nonzero if it isn't a PNG.
*
* The function check_if_png() shown here, but not used, returns nonzero (true)
* if the file can be opened and is a PNG, 0 (false) otherwise.
*
* ...
*/
#define PNG_BYTES_TO_CHECK 4
int check_if_png(char *file_name, FILE **fp)
{
char buf[PNG_BYTES_TO_CHECK];
/* Open the prospective PNG file. */
if ((*fp = fopen(file_name, "rb")) == NULL)
return 0;
/* Read in some of the signature bytes */
if (fread(buf, 1, PNG_BYTES_TO_CHECK, *fp) != PNG_BYTES_TO_CHECK)
return 0;
/* Compare the first PNG_BYTES_TO_CHECK bytes of the signature.
Return nonzero (true) if they match */ |
J'utilise comme option de recherche:
1 2 3
| boost::match_flag_type flags = boost::match_default;
while(boost::regex_search(start, end, what, expression, flags)) |
Merci pour votre aide!
Partager