V
Vittal
Hello All,
I am going the FAQ of perl. In perlfaq6, a method has been given to
remove C/C++ comments. I am not understanding how to use it. Can
somebody help me in understanding?
man perlfaq6 ( How do I use a regular expression to strip C style
comments from a file?)
s{
/\* ## Start of /* ... */ comment
[^*]*\*+ ## Non-* followed by 1-or-more *?s
(
[^/*][^*]*\*+
)* ## 0-or-more things which don?t start with
/
## but do end with ?*?
/ ## End of /* ... */ comment
? ## OR various things which aren?t comments:
(
" ## Start of " ... " string
(
\\. ## Escaped char
? ## OR
[^"\\] ## Non "\
)*
" ## End of " ... " string
? ## OR
? ## Start of ? ... ? string
(
\\. ## Escaped char
? ## OR
[^?\\] ## Non ?\
)*
? ## End of ? ... ? string
? ## OR
. ## Anything other char
[^/"?\\]* ## Chars which doesn?t start a comment,
string or escape
)
}{$2}gxs;
A slight modification also removes C++ comments:
s#/\*[^*]*\*+([^/*][^*]*\*+)*/?//[^\n]*?("(\\.?[^"\\])*"??(\\.?[^?\\])*??.[^/"?\\]*)#$2#gs;
I am going the FAQ of perl. In perlfaq6, a method has been given to
remove C/C++ comments. I am not understanding how to use it. Can
somebody help me in understanding?
man perlfaq6 ( How do I use a regular expression to strip C style
comments from a file?)
s{
/\* ## Start of /* ... */ comment
[^*]*\*+ ## Non-* followed by 1-or-more *?s
(
[^/*][^*]*\*+
)* ## 0-or-more things which don?t start with
/
## but do end with ?*?
/ ## End of /* ... */ comment
? ## OR various things which aren?t comments:
(
" ## Start of " ... " string
(
\\. ## Escaped char
? ## OR
[^"\\] ## Non "\
)*
" ## End of " ... " string
? ## OR
? ## Start of ? ... ? string
(
\\. ## Escaped char
? ## OR
[^?\\] ## Non ?\
)*
? ## End of ? ... ? string
? ## OR
. ## Anything other char
[^/"?\\]* ## Chars which doesn?t start a comment,
string or escape
)
}{$2}gxs;
A slight modification also removes C++ comments:
s#/\*[^*]*\*+([^/*][^*]*\*+)*/?//[^\n]*?("(\\.?[^"\\])*"??(\\.?[^?\\])*??.[^/"?\\]*)#$2#gs;