如何计算java文件中的注释行数?

2024-07-04 17:08:39 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我计算java文件中不同行数的代码的一部分。在大多数情况下,这个代码可以得到正确的结果号码。但是如果注释行类似于:

/*。。。。。。。在

*。。。。。。。在

*/

它将把这一块看作只有两条线

        for eachLine in allLines:
            if eachLine != " " :
                eachLine = eachLine.replace(" ",""); #remove space
                eachLine = self.trim(eachLine);      #remove tabIndent
                if  (iscomment==False):
                    if(eachLine.strip().startswith("//")): #LINECOMMENT 
                        commentCount += 1;
                    if eachLine == "":
                        blankCount += 1;
                    if(eachLine.strip().startswith("/*")):
                        commentCount += 1;
                        if(not eachLine.strip().endswith("*/")):
                            iscomment=True
                else :
                    commentCount += 1;
                    if(eachLine.find("*/")):
                        iscomment=False
            lineCount = lineCount + 1;
        codeCount=lineCount-commentCount-blankCount

Tags: 文件代码falseif情况javaremove号码
2条回答

您应该考虑regexp

import re

comments = re.findall('#.*?\n', allLines)
for item in re.findall('/\*.?*\*/', allLines):
    for row in item.split('\n'):
        comments.append(row)
print(len(comments))

我在一个非常简单的项目上试过,仅仅是这几行就行了,似乎能找到合适的线数。在

if(eachLine.find("*/")):

失败时返回-1,而不是false。在

http://docs.python.org/2/library/string.html

相关问题 更多 >

    热门问题