python regex to match multi-line preprocessor macro -


what follows regular expression have written match multi-line pre-processor macros in c / c++ code. i'm no means regular expressions guru, i'd welcome advice on how can make better.

here's regex:

\s*#define(.*\\\n)+[\s]+(?!\\) 

it should match of this:

#define foo(x) if(x) \ dosomething(x) 

but of (shouldn't match next line of code:

#define foo(x) if(x) \ dosomething(x) normalcode(); 

and shouldn't match single-line preprocessor macros.

i'm pretty sure regex above works - said, there better way of doing it, , imagine there ways of breaking it. can suggest any?

this simple test program knocked up:

#!/usr/bin/env python  test1=""" #include "foo.h" #define bar foo\\     x #include "bar.h" """  test2=""" #define bar foo #define x 1 \\     12 \\     2 \\\\ 3 foobar """  test3=""" #define foo(x) if(x) \\ dosomething(x) """  test4=""" #define foo(x) if(x) \\ dosomething(x) normalcode(); """  import re matcher = re.compile(r"^[ \t]*#define(.*\\\n)+.*$",re.multiline)  def extractdefines(s):     mo = matcher.search(s)     if not mo:         print mo         return     print mo.group(0)  extractdefines(test1) extractdefines(test2) extractdefines(test3) extractdefines(test4) 

the re used:

r"^[ \t]*#define(.*\\\n)+.*$" 

is similar 1 use used, changes:

  1. [ \t] avoid newlines @ start of define.
  2. i rely on + being greedy, can use simple .*$ @ end first line of define doesn't end \

Comments

Popular posts from this blog

windows - Why does Vista not allow creation of shortcuts to "Programs" on a NonAdmin account? Not supposed to install apps from NonAdmin account? -

c++ - How do I get a multi line tooltip in MFC -

unit testing - How to mock PreferenceManager in Android? -