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. 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.
EDIT: Ok, after your edit, Geo, it is this:
^(?<!\/\/)\s*void aMethod
If you simply want to find all appearances for 'void aMethod(params' then you can use the following:
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)
Try this regexp.
(\/\/)?\s*void aMethod
For the three options: (?:\/\/)?\s*void aMethod
精彩评论