views:

125

answers:

1

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.)

+2  A: 

I think you're making it more difficult than it needs to be. Try this:

/
  (?<!\\)    # not preceded by a backslash
  (?:\\\\)*  # zero or more escaped backslashes
  \\ \n      # single backslash and linefeed
/x
Alan Moore
That's exactly what I was about to suggest. +1
Tomalak
Man, that’s so obvious. Thanks a lot for putting me straight.
Konrad Rudolph