posts - 195,  comments - 30,  trackbacks - 0
注意,我们只是简单的提取// 以及/* */之间的内容。
如果 程序中出现了“/*”,会有bug

#!/usr/bin/env python

import sys
import re

def comment_finder(text):
    pattern = re.compile( r'//.*?$|/\*.*?\*/', re.DOTALL | re.MULTILINE)
    result = pattern.findall(text)
    return result

def print_command(filename):

    codefile = open(filename,'r')
    commentfile = open(filename+".txt",'w')
    lines=codefile.read()
    codefile.close()
    #the list of comments
    list_of_comments = comment_finder(lines)
    for comment in list_of_comments:
        #print comment[0:2]
        if comment[0:2] == "//":
                comment_to_write = comment[2:]
        else:
            comment_to_write = comment[2:-2]
        if len(comment_to_write)!=0:
            commentfile.write(comment_to_write)
        commentfile.write('\n')
    commentfile.close()

if __name__ == "__main__":
    for filename in sys.argv[1:]:
        print_command(filename)
使用:

在linux下面 转到当前目录 ./get_comment.py *
或者 指定文件类型
./get_comment.py *.c
posted on 2012-12-03 08:35 luis 阅读(1263) 评论(0)  编辑 收藏 引用 所属分类: Python

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


<2010年12月>
2829301234
567891011
12131415161718
19202122232425
2627282930311
2345678

常用链接

留言簿(3)

随笔分类

随笔档案

文章分类

文章档案

友情链接

搜索

  •  

最新评论

阅读排行榜

评论排行榜