我问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
答案 0 :(得分:3)
这应该为你的例子做点什么:
re.search("^(?!\/\/)\s*void aMethod",line)
答案 1 :(得分:1)
是否有特殊需要使用正则表达式?如果没有,您也可以尝试使用以下内容:
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评论编辑
答案 2 :(得分:1)
如果您想忽略评论,我建议您首先“预处理”您的文件以忽略/删除评论。见:Python snippet to remove C and C++ comments