代码之家  ›  专栏  ›  技术社区  ›  Konrad Rudolph

匹配数量不均匀的转义符号

  •  3
  • Konrad Rudolph  · 技术社区  · 16 年前

    我需要匹配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规则适用,但我希望您能给出以下答案: 任何 Regex方言。)

    1 回复  |  直到 16 年前
        1
  •  5
  •   Alan Moore Chris Ballance    16 年前

    /
      (?<!\\)    # not preceded by a backslash
      (?:\\\\)*  # zero or more escaped backslashes
      \\ \n      # single backslash and linefeed
    /x