I need to match C++ preprocessor statements. Now, preprocessor statements may span multiple lines:
#define foobar \
"something glorious"
This final backslash may be escaped so the following results in two separate lines:
#define foobar \\
No longer in preprocessor.
The question is how I can match the explicit line continuation efficiently. I have the following expression which I think works. Basically, it tests whether the number of backslashes is odd. Is this correct? Can it be done more efficiently?
/
[^\\] # Something that's not an escape character, followed by …
(?<escape>\\*?) # … any number of escapes, …
(?P=escape) # … twice (i.e. an even number).
\\ \n # Finally, a backslash and newline.
/x
(I'm using PHP so PCRE rules apply but I'd appreciate answers in any Regex vernacular.)