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.

Geo
  • 93,257
  • 117
  • 344
  • 520
  • 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 '11 at 08: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 '11 at 08: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 '11 at 08: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: http://pygments.org/docs/api/#lexers – Udi Jun 22 '11 at 08:39

4 Answers4

7

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

^(?<!\/\/)\s*void aMethod
Leif
  • 2,143
  • 2
  • 15
  • 26
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)
Artsiom Rudzenka
  • 27,895
  • 4
  • 34
  • 52
1

Try this regexp.

(\/\/)?\s*void aMethod
Timofey Stolbov
  • 4,501
  • 3
  • 40
  • 45
1

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

Udi
  • 29,222
  • 9
  • 96
  • 129