2010-04-23 179 views
2

我正在尝试使用正则表达式来提取文件标题中的注释。正则表达式问题

例如,源代码可能看起来像:

//This is an example file. 
//Please help me. 

#include "test.h" 
int main() //main function 
{ 
    ... 
} 

我想从代码中提取的前两行,即

//This is an example file. 
//Please help me. 

任何想法?

+0

如何定义“文件的标题”?它是否包含所有注释行直到第一个未注释的行?或者直到第一个“包含”(可能并不总是存在)?空行是否标记标题的结尾? ''//之前是否有空格或制表符?是否允许其他种类的评论?/ * * /'? – 2010-04-25 13:08:48

回答

2
>>> code="""//This is an example file. 
... //Please help me. 
... 
... #include "test.h" 
... int main() //main function 
... { 
... ... 
... } 
... """ 
>>> 
>>> import re 
>>> re.findall("^\s*//.*",code,re.MULTILINE) 
['//This is an example file.', '//Please help me.'] 
>>> 

如果您只需要匹配顶部的连续注释行,则可以使用以下内容。

>>> re.search("^((?:\s*//.*\n)+)",code).group().strip().split("\n") 
['//This is an example file.', '//Please help me.'] 
>>> 
+1

这将提供文件中的所有注释行。它不会提取标题。 – Stephen 2010-04-23 14:46:43

+0

@Stephen,我为此添加了另一个正则表达式。 – YOU 2010-04-23 15:08:07

5

为什么使用正则表达式?

>>> f = file('/tmp/source') 
>>> for line in f.readlines(): 
... if not line.startswith('//'): 
...  break 
... print line 
... 
+1

正则表达式应该是*最后*度假村。根据我的经验,我看到的95%的正则表达式用法可以用类似Stephen提供的方式进行简化。 – Escualo 2010-04-23 20:48:54

+0

代码需要稍微修改,以便在第一行未注释或注释行在它们之间有多行时不会退出。 – Escualo 2010-04-23 20:51:37

+0

行以“//”... egad开头。 – 2010-04-24 02:36:15

1

这不只是获得第2个注释行,但mulitline和//评论在后面为好。它不是你要求的。

data=open("file").read() 
for c in data.split("*/"): 
    # multiline 
    if "/*" in c: 
     print ''.join(c.split("/*")[1:]) 
    if "//" in c: 
     for item in c.split("\n"): 
      if "//" in c: 
      print ''.join(item.split("//")[1:]) 
+0

这对我来说没有意义,为什么只有前两行,无论如何+1 – YOU 2010-04-23 15:18:23

0

上下文延伸进入下面的考虑

  1. 空间中的//前面...
  2. 空行,每行之间// ...线

import re 

code = """//This is an example file.  
a 
    // Please help me. 

// ha 

#include "test.h" 
int main() //main function 
{ 
    ... 
}""" 

for s in re.finditer(r"^(\s*)(//.*)",code,re.MULTILINE): 
    print(s.group(2)) 

>>> 
//This is an example file.  
// Please help me. 
// ha