2011-06-22 50 views
0

我问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 
+4

这些是唯一的三种情况吗?怎么样:'/ * foo void aMethod(params)bar * /'(多行注释)和'“foo void aMethod(params)bar”'(字符串文字) –

+0

Python没有标记器吗? – KingCrunch

+0

@Bart,多行不会出现。 – Geo

回答

3

这应该为你的例子做的东西:

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

我只是好奇,会有一个负面的看后面的解决方案吗? – Geo

+0

@Geo,不,因为背后的外观必须有固定的宽度。所以你不能把'\ s *'或'\ s +'放在一个后视中。 –

+0

是的,我知道固定宽度。但是不能用+来重复后视效果,就像后视太空一样? – Geo

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

这将排除'void aMethod(params //',它应该包含在内,并且包括'var =”asdf aMethod asdf“',这应该被排除。 ()“和”void aMethod(params“in line”)。请记住,这只适用于没有多余空格的情况,像这样:void aMethod(params'。 –

+0

@ lazyr,谢谢,编辑 –

相关问题