【问题标题】:Regexp for selecting spaces between digits and decimal char用于选择数字和十进制图表之间的空格的正则表达式
【发布时间】:2011-02-27 00:02:37
【问题描述】:
我想从空格前面有数字或“。”的字符串中删除空格。并以数字或“.”作为后缀。我有这样的字符串:“50 .10”、“50 . 10”、“50. 10”,我希望它们都变成“50.10”,但两边的位数未知。我正在尝试使用这样的前瞻/后瞻断言:
$row = str_replace("/(?<=[0-9]+$)\s*[.]\s*(?=[0-9]+$)/", "", $row);
但它不起作用......
【问题讨论】:
标签:
php
regex
replace
lookahead
lookbehind
【解决方案1】:
也许很简单
$row = preg_replace('#(\d+)\s*\.\s*(\d+)#', '$1.$2', $row);
够了吗?
【解决方案2】:
$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"