0

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.

4
  • 3
    What do you want to achieve with the negative look behind (?<!\/\/) it implies that you want not to match on comment lines, but that would mean it should only match the first case.
    – stema
    Jun 22, 2011 at 8:21
  • In my test the regex matches the first and the third case, like expected. Geo, are you sure, it matches the first and second case?
    – Leif
    Jun 22, 2011 at 8:25
  • Oh damn! I didn't express myself clearly. I only wanted to match if not within a comment. I will ask another question.
    – Geo
    Jun 22, 2011 at 8:25
  • If you want to ignore comments, I suggest to "preprocess" your file to ignore/remove comments as a first step. If you process the file line by line, just check if it is a comment line before searching for your method. See also pygments' clexer: pygments.org/docs/api/#lexers
    – Udi
    Jun 22, 2011 at 8:39

4 Answers 4

7

EDIT: Ok, after your edit, Geo, it is this:

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

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)
1

Try this regexp.

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

For the three options: (?:\/\/)?\s*void aMethod

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.