用于搜索不在C ++注释中的标记的正则表达式

时间:2015-06-12 15:35:04

标签: regex

我有一个包含数千个文件的代码库,并且需要grep查找存在某个令牌Q_OBJECT但不在注释中的标头。这包括单行//条评论和多行/* ... */评论。

此搜索的正则表达式是什么?

1 个答案:

答案 0 :(得分:1)

这应该有效。

进行全局搜索,如果匹配,则返回 要么:

  • 评论第1组
  • 引用字符串或非令牌文本第2组
  • 令牌文字第3组

您只关心捕获组3是否匹配,它包含令牌。

   # (/\*[^*]*\*+(?:[^/*][^*]*\*+)*/|//(?:[^\\]|\\\n?)*?\n)|("(?:\\[\S\s]|[^"\\])*"|'(?:\\[\S\s]|[^'\\])*'|(?!Q_OBJECT)[\S\s](?:(?!Q_OBJECT)[^/"'\\])*)|(Q_OBJECT)
   # '(/\*[^*]*\*+(?:[^/*][^*]*\*+)*/|//(?:[^\\\]|\\\\\n?)*?\n)|("(?:\\\[\S\s]|[^"\\\])*"|\'(?:\\\[\S\s]|[^\'\\\])*\'|(?!Q_OBJECT)[\S\s](?:(?!Q_OBJECT)[^/"\'\\\])*)|(Q_OBJECT)'
   # "(/\\*[^*]*\\*+(?:[^/*][^*]*\\*+)*/|//(?:[^\\\\]|\\\\\\n?)*?\\n)|(\"(?:\\\\[\\S\\s]|[^\"\\\\])*\"|'(?:\\\\[\\S\\s]|[^'\\\\])*'|(?!Q_OBJECT)[\\S\\s](?:(?!Q_OBJECT)[^/\"'\\\\])*)|(Q_OBJECT)"

   (                                # (1 start), Comments 
        /\*                              # Start /* .. */ comment
        [^*]* \*+
        (?: [^/*] [^*]* \*+ )*
        /                                # End /* .. */ comment
     |  
        //                               # Start // comment
        (?: [^\\] | \\ \n? )*?           # Possible line-continuation
        \n                               # End // comment
   )                                # (1 end)
|  
   (                                # (2 start), Non - comments 
        "
        (?: \\ [\S\s] | [^"\\] )*        # Double quoted text
        "
     |  '
        (?: \\ [\S\s] | [^'\\] )*        # Single quoted text
        ' 
     |  
        (?! Q_OBJECT )
        [\S\s]                           # Any other char, but not these special tokens
                                         # Chars which doesn't start a comment, string, escape,
                                         # or line continuation (escape + newline)
        (?:                              # But not these special tokens
             (?! Q_OBJECT )
             [^/"'\\] 
        )*
   )                                # (2 end)
|  
   (                                # (3 start), Special Tokens
        Q_OBJECT                    
   )                                # (3 end)