http://www.perlmonks.org?node_id=927848


in reply to extract comments from source code.

To extract the lines between and including ones that start with <DTC> and end with </DTC>:

#!/usr/bin/perl -w use strict; while (<DATA>) { if (m|^\s*<DTC>| ... m|^\s*</DTC>|) { print; } } __DATA__ ...is your data consisting of the C program...
See: tutorial on Flip/Flop operator: Flipin good, or a total flop?

Lines extracted:

<DTC> <TroubleCode>1101</TroubleCode> <ShortDescription>The reductant pump needs to run slower than expect +ed to keep the pressure.</ShortDescription> <Classification>FAULT</Classification> <SelfHealing>No selfhealing</SelfHealing> <Erasability>erasable</Erasability> <RedetectedDegradation>No Degradation</RedetectedDegradation> <WarningLamp>No Warning Lamp</WarningLamp> <DirectDegradation>No Action</DirectDegradation> <Order>PRIMARY</Order> </DTC>
There are a number of Perl XML parsers that can deal with these lines after they have been extracted from the C code.

Update:
I looked again at the post and I think that the "C" code will not compile because the <DTC>...</DTC> stuff does not look like a comment block to the C compiler. I guess the blocks could be like:

/*XML doc start <DTC> <TroubleCode>1101</TroubleCode> <Classification>FAULT</Classification> <SelfHealing>No selfhealing</SelfHealing> <Erasability>erasable</Erasability> </DTC> XML doc end */
So that the C compiler will see the whole XML doc as a single comment block.

An alternate that is worth considering is Perl POD (Perl Plain Old Documentation) Perl POD. If you organize the "comments" in the C code right, the C compiler will be happy. And the Perl documentation generator will also be happy processing the raw C input file (a "=" as the first character on a line has special meaning). The Perl POD generators will make HTML web pages or man pages directly from the C source file, if the comments /*....*/ are done right.

/*Perl doc start =some heading in Perl POD ... lines =some other heading tag in Perl POD ..... lines =cut Perl doc end */

Replies are listed 'Best First'.
Re^2: extract comments from source code.
by veerubiji (Sexton) on Sep 26, 2011 at 15:53 UTC
    Hi Marshall, Thank you very much for your reply.I tried your suggested regex its working. I am very happy with that and i am at learning stage so I got some understanding with your code that extract xml comments. also thank you very much for your suggetion in c code also i rectified that error also. now i am very happy because my problem solved. If incase i have more question on perl programming i will ask you.,because i am working with that. thank you , regards, veerubiji

      Hi marshall, i have one more problem i have xml file like below. but i nedd to create hash table using perl script.

      <code>