Php 是否可以在正则表达式中添加和两个单独的lookaround/zero-width断言(即lookbehind/lookbehind)?

Php 是否可以在正则表达式中添加和两个单独的lookaround/zero-width断言(即lookbehind/lookbehind)?,php,regex,concatenation,pcre,lookbehind,Php,Regex,Concatenation,Pcre,Lookbehind,我正在使用Perl来解决这个正则表达式问题,但最好知道它是否也适用于PHP 我需要注释掉PHP文件中所有的print语句或所有以print开头的内容。它看起来像这样: <?php // Description of file ... print("Foobar"); // print("Foo"); //print("bar"); // Open and print file function printtemplate($file)

我正在使用Perl来解决这个正则表达式问题,但最好知道它是否也适用于PHP

我需要注释掉PHP文件中所有的print语句或所有以print开头的内容。它看起来像这样:

<?php
    // Description of file
    ...
    print("Foobar");
    // print("Foo");
    //print("bar");
    // Open and print file
    function printtemplate($file) {
    ...
    }
    ...
    printtemplate($file);
    ...  
?>
((?<!function )|(?<!//))print

任何帮助都将不胜感激。谢谢。

把它们放在一起就行了。就这样。它将创造和影响,因为你需要通过这两个环顾四周,然后才能匹配他们后面的任何东西

在您的情况下,它将是:

(?<!function )(?<!//)print
(?
但是,请注意,上面的正则表达式将返回假阳性,这将导致添加更多的注释


用于PCRE(用于PHP),look-behind断言要求模式的长度严格固定,因此不可能在所有情况下都使用look-behind断言来检查
print
是否被注释掉以排除它。@mpapec的回答给出了一个适用于编写良好的代码的解决方案,并且比使用look-be的正则表达式具有更好的覆盖范围hind.

这是一种简单的方法,适用于给定的示例

s|^ (\s*) (print) |$1//$2|xmg;
s|^ (\s*) (print) |$1//$2|xmg;