2014-03-27 34 views
0

這是我的代碼的一部分來計算java文件中不同行的數量。在大多數情況下,這個代碼可以得到正確的number.But如果註釋行是這樣的:如何計算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 
+0

看看[plyj](https://github.com/musiKk/plyj) – Selcuk

回答

0

你應該考慮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)) 

東西的臺詞,試了一下我的一個非常簡單的項目,它似乎取線的正確ammount的。