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。我只是在搜索解决方案时遇到了这个问题。我用这个来检查正则表达式是否工作: