Geo
Geo

Reputation: 96837

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.

Upvotes: 0

Views: 111

Answers (4)

Leif
Leif

Reputation: 2170

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

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

Upvotes: 7

Artsiom Rudzenka
Artsiom Rudzenka

Reputation: 29111

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)

Upvotes: 2

Udi
Udi

Reputation: 30512

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

Upvotes: 1

Timofey Stolbov
Timofey Stolbov

Reputation: 4621

Try this regexp.

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

Upvotes: 1

Related Questions