0

I asked this question earlier but I didn't express myself correctly. If I have these three cases:

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

How could I tweak my regex to match only if the string is not found in a comment? This is my regex:

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

Would this catch everything:

(?<!\/\/)(?<!\s)+void onMouseReleased
Community
  • 1
  • 1
Geo
  • 93,257
  • 117
  • 344
  • 520
  • 4
    Are those the only three cases? What about: `/*foo void aMethod(params) bar*/` (multi-line comment) and `"foo void aMethod(params) bar"` (string literal) – Bart Kiers Jun 22 '11 at 08:37
  • Doesn't Python have a tokenizer? – KingCrunch Jun 22 '11 at 08:42
  • @Bart, multiline will not appear. – Geo Jun 22 '11 at 08:44
  • @Geo, and string literals that might possibly contain text that look like method calls? – Bart Kiers Jun 22 '11 at 08:48
  • Plz, tell us more about your problem. There is a good chance, it'll be insane to solve with regexps. BTW, http://pypi.python.org/pypi/pygccxml, http://code.google.com/p/pycparser/. – alex vasi Jun 22 '11 at 08:49
  • I'm looking in a lot of source files to see if 2 methods exist. They do, in most files. I'm trying to find the ones in which they don't. – Geo Jun 22 '11 at 08:57

3 Answers3

3

This should do the stuff for your examples :

re.search("^(?!\/\/)\s*void aMethod",line)
Cédric Julien
  • 78,516
  • 15
  • 127
  • 132
  • I'm just curious, would a negative lookbehind solution be possible? – Geo Jun 22 '11 at 08:53
  • @Geo, no, since the look behind must have a fixed width. So you can't put `\s*` or `\s+` inside a look-behind. – Bart Kiers Jun 22 '11 at 08:56
  • Yes, I know about the fixed width. But couldn't the look-behinds be repeated with a +, like a look-behind for space? – Geo Jun 22 '11 at 08:58
  • You should perhaps allow for extraneous whitespace elsewhere too. And searching for start-of-line and then immediately negative lookahead is the same as using match and just not including `//` in the regexp: `re.match("\s*void\s+aMethod\s*\(\s*params")`. – Lauritz V. Thaulow Jun 22 '11 at 09:34
1

Is there any particular need to use regex? If not you can also try to use the following:

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

for line in a.split('\n'):
    if not line.strip().startswith("//") and "void aMethod(params" in line:
        print line

Edited as per lazyr comment

Artsiom Rudzenka
  • 27,895
  • 4
  • 34
  • 52
  • 2
    This will exclude `void aMethod(params //`, which should be included, and include `var = "asdf aMethod asdf"`, which should be excluded. Do `if not line.strip().startswith("//") and "void aMethod(params" in line` instead. Keep in mind this only works if there are no extraneous whitespace here and there, like this: `void aMethod( params`. – Lauritz V. Thaulow Jun 22 '11 at 09:24
1

If you want to ignore comments, I suggest to "preprocess" your file to ignore/remove comments as a first step. see: Python snippet to remove C and C++ comments

Community
  • 1
  • 1
Udi
  • 29,222
  • 9
  • 96
  • 129