用于选择数字和小数字符之间的空格的正则表达式

发布于 2024-09-03 13:24:17 字数 244 浏览 6 评论 0原文

我想从字符串中删除空格,其中空格前面有数字或“。”并添加数字或“.”。我有这样的字符串:“50 .10”、“50 . 10”、“50. 10”,我希望它们全部变成“50.10”,但两边的数字位数未知。我正在尝试使用像这样的前瞻/后瞻断言:

$row = str_replace("/(?<=[0-9]+$)\s*[.]\s*(?=[0-9]+$)/", "", $row);

但它不起作用......

I want to remove spaces from strings where the space is preceeded by a digit or a "." and acceded by a digit or ".". I have strings like: "50 .10", "50 . 10", "50. 10" and I want them all to become "50.10" but with an unknown number of digits on either side. I'm trying with lookahead/lookbehind assertions like this:

$row = str_replace("/(?<=[0-9]+$)\s*[.]\s*(?=[0-9]+$)/", "", $row);

But it does not work...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风向决定发型 2024-09-10 13:24:19

也许一个简单的

$row = preg_replace('#(\d+)\s*\.\s*(\d+)#', '$1.$2', $row);

就足够了?

Maybe a simple

$row = preg_replace('#(\d+)\s*\.\s*(\d+)#', '$1.$2', $row);

could suffice?

蓝海似她心 2024-09-10 13:24:19
$str = '50 .10, 50 . 10, 50. 10';
$str = preg_replace('/(\d+)\s*\.\s*(\d+)/', '$1.$2', $str);
echo($str);  // results in "50.10, 50.10, 50.10"
$str = '50 .10, 50 . 10, 50. 10';
$str = preg_replace('/(\d+)\s*\.\s*(\d+)/', '$1.$2', $str);
echo($str);  // results in "50.10, 50.10, 50.10"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文