<p>What follows is a regular expression I have written to match multi-line pre-processor macros in C / C++ code. I'm by no means a regular expressions guru, so I'd welcome any advice on how I can make this better.</p>

<p>Here's the regex:</p>

<pre><code>\s*#define(.*\\\n)+[\S]+(?!\\)
</code></pre>

<p>It should match all of this:</p>

<pre><code>#define foo(x) if(x) \
doSomething(x)
</code></pre>

<p>But only some of this (shouldn't match the next line of code:</p>

<pre><code>#define foo(x) if(x) \
doSomething(x)
normalCode();
</code></pre>

<p>And also shouldn't match single-line preprocessor macros.</p>

<p>I'm pretty sure that the regex above works - but as I said, there probably a better way of doing it, and I imagine that there are ways of breaking it. Can anyone suggest any?</p>

This is a simple test program I 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 I used:
```
```
r"^[ \t]*#define(.*\\\n)+.*$"
```
```
Is very similar to the one use used, the changes:
[ \t] To avoid newlines at the start
of the define.
I rely on + being
greedy, so I can use a simple .*$ at
the end to get the first line of the
define that doesn't end with \