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
  • 89,506
  • 114
  • 330
  • 511
  • 3
    What do you want to achieve with the negative look behind `(? – 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,053
  • 2
  • 15
  • 25
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
  • 26,491
  • 4
  • 32
  • 51
1

Try this regexp.

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

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

Udi
  • 27,155
  • 8
  • 91
  • 123