如何找到不在注释中的方法名?

2024-04-20 02:34:13 发布

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

我问了this question earlier,但我没有正确地表达自己。如果我有这三个案子:

void aMethod(params ...)
//void aMethod(params
// void aMethod(params
  ^ can have any number of spaces here

如果在注释中找不到字符串,如何调整正则表达式使其匹配?这是我的正则表达式:

re.search("(?<!\/\/)\s*void aMethod",buffer)

这能抓住一切吗:

(?<!\/\/)(?<!\s)+void onMouseReleased

Tags: of字符串numberherehaveanyparamsthis
3条回答

如果你想忽略评论,我建议你“预处理”你的文件忽略/删除评论作为第一步。见:Python snippet to remove C and C++ comments

这应该可以为你的例子做一些事情:

re.search("^(?!\/\/)\s*void aMethod",line)

有没有特别需要使用regex?如果没有,您也可以尝试使用以下方法:

a = """void aMethod(params ...)
//void aMethod(params
// void aMethod(params
  ^ can have any number of spaces here"""

for line in a.split('\n'):
    if not line.strip().startswith("//") and "void aMethod(params" in line:
        print line

根据lazyr评论编辑

相关问题 更多 >