我有一个包含数千个文件的代码库,并且需要grep查找存在某个令牌Q_OBJECT
但不在注释中的标头。这包括单行//
条评论和多行/* ... */
评论。
此搜索的正则表达式是什么?
答案 0 :(得分:1)
这应该有效。
进行全局搜索,如果匹配,则返回 要么:
您只关心捕获组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)