我需要匹配C ++预处理器语句。现在,预处理程序语句可能跨越多行:

#define foobar \
    "something glorious"

这个最终的反斜杠可能会被转义,因此以下结果分为两行:

#define foobar \\
No longer in preprocessor.

问题是如何有效地匹配显式行继续。我有以下表达式,我认为有效。基本上,它测试反斜杠的数量是否是奇数。它是否正确?可以更有效地完成吗?

/
    [^\\]           # 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

(我正在使用PHP,所以PCRE规则适用,但我很感激任何正则表达式白话的答案。)

有帮助吗?

解决方案

我认为你使它变得比它需要的更难。试试这个:

/
  (?<!\\)    # not preceded by a backslash
  (?:\\\\)*  # zero or more escaped backslashes
  \\ \n      # single backslash and linefeed
/x
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top