我正在尝试使用正则表达式来提取文件标题中的注释。正则表达式问题
例如,源代码可能看起来像:
//This is an example file.
//Please help me.
#include "test.h"
int main() //main function
{
...
}
我想从代码中提取的前两行,即
//This is an example file.
//Please help me.
任何想法?
我正在尝试使用正则表达式来提取文件标题中的注释。正则表达式问题
例如,源代码可能看起来像:
//This is an example file.
//Please help me.
#include "test.h"
int main() //main function
{
...
}
我想从代码中提取的前两行,即
//This is an example file.
//Please help me.
任何想法?
>>> 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.']
>>>
为什么使用正则表达式?
>>> f = file('/tmp/source')
>>> for line in f.readlines():
... if not line.startswith('//'):
... break
... print line
...
这不只是获得第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:])
这对我来说没有意义,为什么只有前两行,无论如何+1 – YOU 2010-04-23 15:18:23
上下文延伸进入下面的考虑
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
如何定义“文件的标题”?它是否包含所有注释行直到第一个未注释的行?或者直到第一个“包含”(可能并不总是存在)?空行是否标记标题的结尾? ''//之前是否有空格或制表符?是否允许其他种类的评论?/ * * /'? – 2010-04-25 13:08:48