我正在尝试理解正则表达式,但是失败得很惨。
我有一个字符串,我想匹配并删除两个括号之间的所有空格。
例如:
This string (is an example).
Would become:
This string (isanexample).
我正在尝试理解正则表达式,但是失败得很惨。
我有一个字符串,我想匹配并删除两个括号之间的所有空格。
例如:
This string (is an example).
Would become:
This string (isanexample).
您可以使用 preg_replace_callback 函数。
$str = "This string (is an example).";
$str = preg_replace_callback("~\(([^\)]*)\)~", function($s) {
return str_replace(" ", "", "($s[1])");
}, $str);
echo $str; // This string (isanexample).
$line = preg_replace_callback(
'/\(.*\)/',
create_function(
"\$matches",
"return preg_replace('/\s+/g','',\$matches);"
),
$line
);
m
修饰符,但不需要。测试:"This string (is an example).\n\t\t\t(foo bar baz) ..." - Kerem