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:
- [ \t] avoid newlines @ start of define.
- i rely on + being greedy, can use simple .*$ @ end first line of define doesn't end \
Comments
Post a Comment