PHP:正则表达式匹配字符串前面没有美元符号

Bri*_*ham 0 php regex ignore operator-precedence

在我的语法高亮显示器中,我使用正则表达式来解析不同的术语.下面是我解析PHP类的方法:

foreach ( PHP::$Classes as $class )
    $code = preg_replace( "/\b{$class}\b/", $this->_getHtmlCode( $class, PHP::$Colors['class'] ), $code );
Run Code Online (Sandbox Code Playgroud)

现在,只需忽略PHP类和_getHtmlCode函数.正则表达式"/\b{$class}\b/"匹配诸如的名称count.如果我创建一个名为的变量$count,那么匹配就好了.

如何查找前面没有的类名$

Nig*_*cat 5

您可以使用负零宽度后视来完成相同的任务 - 基本上,确保在您的文本之前没有美元符号:/(?<!\$){$class}/.

(?<!     # Non-capturing look-behind group, captures only if the following regex is NOT found before the text.
  \$)    # Escaped dollar sign
{$class} # Class name
Run Code Online (Sandbox Code Playgroud)