拆下C/C++;来自使用python的源文件的注释

拆下C/C++;来自使用python的源文件的注释,python,regex,Python,Regex,我有一个正则表达式,它忽略以/*…*/开头的多行注释 但不适用于以开头的行// 有人能建议在这个正则表达式中添加什么让它忽略吗 pattern = r""" ## --------- COMMENT --------- /\* ## Start of /* ... */ comment [^*]*\*+ ## Non-* followed by 1-or-more *'s

我有一个正则表达式,它忽略以/*…*/开头的多行注释 但不适用于以开头的行//

有人能建议在这个正则表达式中添加什么让它忽略吗

pattern = r"""
                        ##  --------- COMMENT ---------
       /\*              ##  Start of /* ... */ comment
       [^*]*\*+         ##  Non-* followed by 1-or-more *'s
       (                ##
         [^/*][^*]*\*+  ##
       )*               ##  0-or-more things which don't start with /
                        ##    but do end with '*'
       /                ##  End of /* ... */ comment
     |                  ##  -OR-  various things which aren't comments:
       (                ## 
                        ##  ------ " ... " STRING ------
         "              ##  Start of " ... " string
         (              ##
           \\.          ##  Escaped char
         |              ##  -OR-
           [^"\\]       ##  Non "\ characters
         )*             ##
         "              ##  End of " ... " string
       |                ##  -OR-
                        ##
                        ##  ------ ' ... ' STRING ------
         '              ##  Start of ' ... ' string
         (              ##
           \\.          ##  Escaped char
         |              ##  -OR-
           [^'\\]       ##  Non '\ characters
         )*             ##
         '              ##  End of ' ... ' string
       |                ##  -OR-
                        ##
                        ##  ------ ANYTHING ELSE -------
         .              ##  Anything other char
         [^/"'\\]*      ##  Chars which doesn't start a comment, string
       )                ##    or escape
    """

如果您计划使用当前的regexp,以下是您可以执行的操作以匹配
/…
注释:

以下是:

 /                ##  End of /* ... */ comment
添加以下内容:

 |                  ## OR it is a line comment with //
  \s*//.*           ## Single line comment

请参见

您使用此功能的目的是什么?真的需要正则表达式吗?在这一点上,您可以停止只使用正则表达式(多行注释不是上下文无关的语法)。我确实使用了自定义解析器来查找C/C++源文件中的原始字符串:看起来在这种情况下,使用pyparsing这样的解析框架可能更易于管理。HI@MichaelSPriz。我正在编写一个python工具,它将两个c/cpp文件之间的注释(包含perforce头信息以及日期和时间修改信息)去掉,以便比较它们并查看代码中是否有更改。