Php preg#u replace不';不匹配正确的组?

Php preg#u replace不';不匹配正确的组?,php,regex,Php,Regex,我想编写一个将忽略(比如)的正则表达式: 或,但将捕获和或。 我可以通过以下方式完成: <(?!\/)(?!(i|div)(\s|>))(.*?)> )(**> 但当我尝试返回第三组(.*)时,它似乎没有返回: $text = preg_replace("/<(?!\/)(?!(i|div)(\s|>))(.*?)>/i", "< $3>", // note I added spac

我想编写一个将忽略(比如)的正则表达式:

或,但将捕获和或。
我可以通过以下方式完成:

<(?!\/)(?!(i|div)(\s|>))(.*?)>
)(**>
但当我尝试返回第三组(.*)时,它似乎没有返回:

$text = preg_replace("/<(?!\/)(?!(i|div)(\s|>))(.*?)>/i", 
                            "< $3>",  // note I added space
                            $text);
$text=preg\u replace(“/)(.*)>/i”,
“<$3>”,//注意我添加了空格
$text);
我还试着用1美元和2美元。我错过什么了吗

更新,我一点也不清楚

我使用了以下方法

$text = preg_replace("/<(?!\/)(?!(i|div|p)(\s|>))(.*?)>/i", 
                        "< ${3}>",
                        $text);
$text=preg\u replace(“/)(.*)>/i”,
"< ${3}>",
$text);
关于:

power中的测试注释

据我所知,我应该得到:

<p>test test test test annotation in < inline_code>power</inline_code>.</p>
电源中的测试注释

但是当我在服务器上运行它时,我得到

<p>test test test test annotation in power.</p>
power中的测试注释


您的代码在我的控制台上运行良好,我尝试过的每种变体;单引号,双引号,直接从你的文章中复制

php > $pattern = '/<(?!\/)(?!(i|div)(\s|>))(.*?)>/';                            
php > $subject = '<div> or <i> or <div class="something"> but will catch <divdiv> and <iii> or <hello>.';
php > $text = preg_replace($pattern,
php ( "<$3 something>",
php ( $subject);
php > print $text;
<div> or <i> or <div class="something"> but will catch <divdiv something> and <iii something> or <hello something>.


php > $text = '<div> or <i> or <div class="something"> but will catch <divdiv> and <iii> or <hello>';
php > $text = preg_replace("/<(?!\/)(?!(i|div)(\s|>))(.*?)>/i", 
php (                             "< $3>",  // note I added space
php (                             $text);
php > print $text;
<div> or <i> or <div class="something"> but will catch < divdiv> and < iii> or < hello>
php>$pattern='/)(.*?>/);
php>$subject='或,但将捕获和或';
php>$text=preg_replace($pattern,
php(“”,
php(主题);
php>打印$text;
或但会抓住和或。
php>$text='或,但将捕获和或';
php>$text=preg_replace(“/)(.*?”>/i“,
php(“<$3>”,//注意我添加了空格
php($文本);
php>打印$text;
或但将捕获
但您更新的示例有一个问题:

php > $text = '<p>test test test test annotation in <inline_code>power</inline_code>.</p>';
php > $text = preg_replace("/<(?!\/)(?!(i|div|p)(\s|>))(.*?)>/i", 
php (                         "< ${3}>",
php (                         $text);
PHP Notice:  Undefined variable: 3 in php shell code on line 2
php>$text='测试power中的注释。

'; php>$text=preg_replace(“/)(.*?”>/i“, php(“<${3}>”, php($文本); PHP注意:第2行PHP外壳代码中的未定义变量:3

我正在运行PHP7

双引号与单引号?我正在使用php版本5.5.36,我在5.5.9.Works上试用过。${3}但是,在双引号中会失败,因为php会在将其传递给函数之前尝试将其解释为变量表达式。这是令人困惑的。原始代码没有问题,留下了什么…?数据的有效性、设置的差异,?这里是一个完全不使用任何反向引用的模式:
$text=preg_replace('/)(?=.*?>)/i','<',$text)成功了吗?我认为围绕模式的单引号确实起到了作用。否则它必须是\\s而不是\s等。感谢您的尝试。我更新了我的问题,现在将其改为双引号。我不明白为什么这不起作用……我建议对模式和替换使用单引号,\3而不是3美元。iirc旧php只能处理\way。此外,在preg_replace页面中,它再次使用了$作为首选方式,而changelog没有提到它。我今天帮不了你,抱歉:DWait,我只是对你测试的数据做一个假设。请原谅我问了这么基本的问题,但是你确定你正在测试你认为正在测试的数据吗?
php > $pattern = '/<(?!\/)(?!(i|div)(\s|>))(.*?)>/';                            
php > $subject = '<div> or <i> or <div class="something"> but will catch <divdiv> and <iii> or <hello>.';
php > $text = preg_replace($pattern,
php ( "<$3 something>",
php ( $subject);
php > print $text;
<div> or <i> or <div class="something"> but will catch <divdiv something> and <iii something> or <hello something>.


php > $text = '<div> or <i> or <div class="something"> but will catch <divdiv> and <iii> or <hello>';
php > $text = preg_replace("/<(?!\/)(?!(i|div)(\s|>))(.*?)>/i", 
php (                             "< $3>",  // note I added space
php (                             $text);
php > print $text;
<div> or <i> or <div class="something"> but will catch < divdiv> and < iii> or < hello>
php > $text = '<p>test test test test annotation in <inline_code>power</inline_code>.</p>';
php > $text = preg_replace("/<(?!\/)(?!(i|div|p)(\s|>))(.*?)>/i", 
php (                         "< ${3}>",
php (                         $text);
PHP Notice:  Undefined variable: 3 in php shell code on line 2