<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: comment code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 17 May 2008 22:31:24 GMT</pubDate>
    <description>DZone Snippets: comment code</description>
    <item>
      <title>C++ Style Comment Stripper</title>
      <link>http://snippets.dzone.com/posts/show/3546</link>
      <description>// This scirpt either removes the C++ style comments or places them&lt;br /&gt;// just before the line in which they occur&lt;br /&gt;// for ex:&lt;br /&gt;//&lt;br /&gt;//      int i=0, sum;    //This is some comment(embedded)&lt;br /&gt;//&lt;br /&gt;// after conversion:&lt;br /&gt;//      //This is some comment(embedded)&lt;br /&gt;//      int i=0, sum;&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;# comm_stripper&lt;br /&gt;#&lt;br /&gt;# Description :&lt;br /&gt;#&lt;br /&gt;#   This scirpt either removes the C++ style comments or places them&lt;br /&gt;#   just before the line in which they occur&lt;br /&gt;#   for ex:&lt;br /&gt;#&lt;br /&gt;#       int i=0, sum;    //This is some comment(embedded)&lt;br /&gt;#&lt;br /&gt;#   after conversion:&lt;br /&gt;#       //This is some comment(embedded)&lt;br /&gt;#       int i=0, sum;&lt;br /&gt;&lt;br /&gt;import re&lt;br /&gt;import os&lt;br /&gt;&lt;br /&gt;#comm_re = re.compile(r"(\s*)(.*)(//.*)")&lt;br /&gt;&lt;br /&gt;comm_re = re.compile(r"(\s*)(.*?)(//.*)")&lt;br /&gt;ext_re  = re.compile(r"\.(cpp|h|hpp|c|icc)$")&lt;br /&gt;out_ext = ".out"&lt;br /&gt;&lt;br /&gt;TRUE  = 1&lt;br /&gt;FALSE = 0&lt;br /&gt;&lt;br /&gt;def comm_strip(file_name, keep_comment=TRUE):&lt;br /&gt;    lines = file(file_name).readlines()&lt;br /&gt;    out_lines = []&lt;br /&gt;&lt;br /&gt;    out_filename = file_name + out_ext&lt;br /&gt;    fout = open(out_filename, "w")&lt;br /&gt;&lt;br /&gt;    for line in lines:&lt;br /&gt;        match = comm_re.match(line.rstrip())&lt;br /&gt;&lt;br /&gt;        if(match):&lt;br /&gt;            if keep_comment:&lt;br /&gt;                #print "%s%s" % (match.group(1), match.group(3))&lt;br /&gt;                out_lines.append("%s%s\n" % (match.group(1), match.group(3)))&lt;br /&gt;            if match.group(2):&lt;br /&gt;                #print "%s%s" % (match.group(1), match.group(2).rstrip())&lt;br /&gt;                out_lines.append("%s%s\n" % (match.group(1), match.group(2).rstrip()))&lt;br /&gt;        else:&lt;br /&gt;            #print line.rstrip()&lt;br /&gt;            out_lines.append(line)&lt;br /&gt;&lt;br /&gt;    out_lines.append("\n\n")&lt;br /&gt;    fout.writelines(out_lines)&lt;br /&gt;    print "File %s =&gt; %s" % (file_name, out_filename)&lt;br /&gt;    print "-"*80&lt;br /&gt;&lt;br /&gt;print os.getcwd()&lt;br /&gt;file_names = os.listdir(".")&lt;br /&gt;flag=TRUE&lt;br /&gt;&lt;br /&gt;for file_name in file_names:&lt;br /&gt;    match_ext = ext_re.search(file_name)&lt;br /&gt;&lt;br /&gt;    if match_ext :&lt;br /&gt;        comm_strip(file_name, flag)&lt;br /&gt;&lt;br /&gt;print "Done!"&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Mon, 19 Feb 2007 14:27:40 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3546</guid>
      <author>abhiwin (Abhi Win)</author>
    </item>
    <item>
      <title>Regular expression to match an HTML comment</title>
      <link>http://snippets.dzone.com/posts/show/3100</link>
      <description>// description of your code here&lt;br /&gt;This snippet uses the non-greedy matcher, and the "m" option to treat strings as multi-lines, so it may not work with all regex parsers.&lt;br /&gt;&lt;br /&gt;&lt;code&gt;&lt;br /&gt;/\&lt;!\s*--(.*?)(--\s*\&gt;)/m&lt;br /&gt;&lt;br /&gt;Examples in Ruby IRB:&lt;br /&gt;irb(main):029:0&gt; html = &lt;&lt;-EOL&lt;br /&gt;irb(main):030:0" &lt;!--  First  Comment   --&lt;br /&gt;irb(main):031:0"       --&gt; Second Comment &lt;!--&lt;br /&gt;irb(main):032:0"       --  Third  Comment   --&gt;&lt;br /&gt;irb(main):033:0" EOL&lt;br /&gt;=&gt; "&lt;!--  First  Comment   --\n      --&gt; Second Comment &lt;!--\n      --  Third  Comment   --&gt;\n"&lt;br /&gt;irb(main):075:0&gt; m = html.match(/\&lt;!\s*--(.*?)(--\s*\&gt;)/m)&lt;br /&gt;=&gt; #&lt;MatchData:0x15915a4&gt;&lt;br /&gt;irb(main):076:0&gt; m[0]&lt;br /&gt;=&gt; "&lt;!--  First  Comment   --\n      --&gt;"&lt;br /&gt;irb(main):077:0&gt; m[1]&lt;br /&gt;=&gt; "  First  Comment   --\n      "&lt;br /&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Thu, 07 Dec 2006 00:45:13 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/3100</guid>
      <author>chao (Chao Lam)</author>
    </item>
  </channel>
</rss>
