简体   繁体   English

如何修改此正则表达式以匹配所有三种情况?

[英]How can I modify this regex to match all three cases?

I want to test if a method appears in a header file. 我想测试一种方法是否出现在头文件中。 These are the three cases I have: 这是我遇到的三种情况:

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

Here's what I have so far: 这是我到目前为止的内容:

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

Buf this will only match the first case, and the second. buf只会匹配第一种情况,第二种情况。 How could I tweak it to have it match the third too? 我该如何调整它使其也与第三个匹配?

EDIT:sorry, but I didn't express myself correctly. 编辑:对不起,但我没有正确表达自己。 I only want to match if not within a comment. 如果不在评论中,我只想匹配。 So sorry. 非常抱歉。

编辑:好的,编辑后,地理位置,是这样的:

^(?<!\/\/)\s*void aMethod

If you simply want to find all appearances for 'void aMethod(params' then you can use the following: 如果您只是想查找“ void aMethod(params)”的所有外观,则可以使用以下方法:

a = """void aMethod(params ...)
//void aMethod(params
// void aMethod(params
  ^ can have any number of spaces here"""
from re import findall
findall(r'\bvoid aMethod\b', a)

OR: 要么:

findall(r'(?:\/\/)?[ ]*void aMethod', a)

试试这个正则表达式。

(\/\/)?\s*void aMethod

对于三个选项: (?:\\/\\/)?\\s*void aMethod

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM