Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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 匹配花括号之间的空格_Php_Regex - Fatal编程技术网

Php 匹配花括号之间的空格

Php 匹配花括号之间的空格,php,regex,Php,Regex,我有一行文字: This is {{some. test}} to see if I can remove spaces 我想要这行使用正则表达式的文本: This is {{some.test}} to see if I can remove spaces 我试图进入正确的方向,虽然我可以用([\t]+[])+匹配所有的多个空格,但我不知道如何匹配,只有当它在{{和}之间时 如何修改当前正则表达式?要删除{…}中的所有空格,您可以将{…}子字符串与{.*}正则表达式

我有一行文字:

This is {{some.     test}} to see     if I can remove spaces
我想要这行使用正则表达式的文本:

This is {{some.test}} to see     if I can remove spaces
我试图进入正确的方向,虽然我可以用
([\t]+[])+
匹配所有的多个空格,但我不知道如何匹配,只有当它在
{{
}
之间时


如何修改当前正则表达式?

要删除
{…}
中的所有空格,您可以将
{…}
子字符串与
{.*}
正则表达式匹配,并用
preg\u replace\u callback
替换这些匹配中的空格:

$re = '~{{.*?}}~s'; 
$str = "This is {{some.     test}} to see     if I can remove spaces"; 
echo preg_replace_callback($re, function($m) {
    return str_replace(" ", "", $m[0]);
}, $str);

s
修饰符也将使
匹配换行符。如果您不需要它(并且只想匹配一行中的
{{…}
子字符串),请删除
s

要替换所有类型的空白,请在回调中使用
\s+
模式匹配1+空白字符的
preg\u replace

preg_replace('~\s+~', '', $m[0])

所以,您需要的正则表达式应该在Vim中工作?不,php。我只是在搜索解决方案时遇到了这个问题。我用这个来检查正则表达式是否工作: