PHP正则表达式排除注释查找错误抑制

PHP正则表达式排除注释查找错误抑制,php,regex,pcre,perldoc,Php,Regex,Pcre,Perldoc,我正在尝试使用正则表达式来查看预先存在的代码库,它似乎在变量引用和函数调用中滥用了php错误抑制字符(@)。因此,我希望搜索整个代码库,以创建所有用法的列表。问题是,大部分代码还包括perldoc,我不确定如何排除明显的注释 大多数perldoc似乎由最少的空格asterix空格来预测。e、 g: /** * @param int $somvar */ 因此,它可以与类似/^\s*\*\s+/的内容合理地一致匹配 我用来查找错误抑制字符用法的正则表达式是: 它的结果是令人满意的

我正在尝试使用正则表达式来查看预先存在的代码库,它似乎在变量引用和函数调用中滥用了php错误抑制字符(
@
)。因此,我希望搜索整个代码库,以创建所有用法的列表。问题是,大部分代码还包括perldoc,我不确定如何排除明显的注释

大多数perldoc似乎由最少的空格asterix空格来预测。e、 g:

  /**
   * @param int $somvar
   */
因此,它可以与类似
/^\s*\*\s+/
的内容合理地一致匹配

我用来查找错误抑制字符用法的正则表达式是:

它的结果是令人满意的,除了拾取所有的perldoc

我试着看了一些负面展望的例子,但似乎没有用我已经尝试过的任何东西来回避perldoc的评论。一个不起作用的例子如下:

(?!\s*[\*\/])(@[\$\w][\w\d]*)
非常感谢您的帮助

您可以使用PHP查找所有@符号,而不是正则表达式。这样,您就可以让PHP自己的内部解析器为您解析文件:

$source_file = 'source_file_to_open.php';
$source = file_get_contents($source_file);
$tokens = token_get_all($source);

// Loop through all the tokens
for ($i=0; $i < count($tokens); $i++) {
    // If the token is equal to @, then get the line number (3rd value in array)
    // of the *following* token because the @ does not have a line number because
    // it's not listed as an array, just a string.
    if ($tokens[$i] == '@') {
        echo "@ found in $source_file on line: {$tokens[$i+1][2]}<br />\n";
    }
}
$source_file='source_file_to_open.php';
$source=file\u get\u contents($source\u file);
$tokens=token\u get\u all($source);
//循环遍历所有令牌
对于($i=0;$i\n”;
}
}

不幸的是,它们中的一些实际上被一些东西使用了——比如条令或其他接口,它们将配置放在带有@symbolswill not a的注释块中?顺便说一句,我尝试了负面外观heads和负面外观behind,但它们没有跳过引号,或者跳过了太多。部分问题是我特别需要在行的开头查找空格asterix。字符后面的注释无关紧要,我也不想跳过带有乘法操作数的行。此时,我只是尝试创建一个使用列表,以便我们可以决定是否值得尝试编辑其中的一些。(此代码库中的一些代码来自joomla的旧版本,它实际上包含数千个代码!)(?)?
$source_file = 'source_file_to_open.php';
$source = file_get_contents($source_file);
$tokens = token_get_all($source);

// Loop through all the tokens
for ($i=0; $i < count($tokens); $i++) {
    // If the token is equal to @, then get the line number (3rd value in array)
    // of the *following* token because the @ does not have a line number because
    // it's not listed as an array, just a string.
    if ($tokens[$i] == '@') {
        echo "@ found in $source_file on line: {$tokens[$i+1][2]}<br />\n";
    }
}