Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/283.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP preg_replace-替换文本部分,但不要太早_Php_Preg Replace - Fatal编程技术网

PHP preg_replace-替换文本部分,但不要太早

PHP preg_replace-替换文本部分,但不要太早,php,preg-replace,Php,Preg Replace,使用: 用于清洁和获取wordtwo $text = preg_replace("/\[\[(.*?)SPACE(.*?)\]\]/im",'$2',$text); 但如果文本之前有[[2],则失败 $text = '..text.. [[wordoneSPACE**wordtwo**]] ..moretext..'; 我怎样才能限制到只有空格词的地方?如果[和]中没有[[…]]您可以使用 $text = '.. [[ ..text(not to cut).. [[wordoneSPACE*

使用:

用于清洁和获取wordtwo

$text = preg_replace("/\[\[(.*?)SPACE(.*?)\]\]/im",'$2',$text);
但如果文本之前有[[2],则失败

$text = '..text.. [[wordoneSPACE**wordtwo**]] ..moretext..';

我怎样才能限制到只有空格词的地方?

如果
[
]
中没有
[[…]]
您可以使用

$text = '.. [[ ..text(not to cut).. [[wordoneSPACE**wordtwo**]] ..moretext..';
请参阅。
[^][]
求反字符类将只匹配除
[
]
之外的字符,并且不会跨越
[…]
边界

否则,请使用:


(?:(?!\[{2})。*?
模式将匹配任何字符,0个或更多重复,但尽可能少,不会启动
[[/code>字符序列,并且不会跨越下一个实体
[[
边界。

可能使用另一个选项

在第一组中,可以使用否定字符类来匹配除方括号或S(如果后跟空格)之外的任何字符

$text = preg_replace("/\[\[((?:(?!\[{2}).)*?)SPACE(.*?)]]/is",'$2',$text);
部分地

  • \[\[
    匹配
    [[
  • Capturegroup 1
    • [^][S]+
      匹配除S以外的任何字符的1+倍,]或[
    • (?:
      非捕获组
      • S(?!配速)
        匹配不后跟配速的S
      • |
      • [^][S]+
        匹配除S以外的任何字符的1+倍,]或[
    • )*+
      关闭组并重复0多次
  • 关闭第1组
  • 空格
    按字面匹配
  • 捕获第2组
    • [^][++
      匹配除
      ]
      [
  • 关闭组
  • \]\]
    匹配
    ]]

它是什么格式?可能已经存在解析器
$text = preg_replace("/\[\[((?:(?!\[{2}).)*?)SPACE(.*?)]]/is",'$2',$text);
\[\[([^][S]++(?:S(?!PACE)|[^][S]+)*+)SPACE([^][]++)\]\]