繁体   English   中英

带点和逗号的正则表达式货币格式

[英]Regular expression currency format with dots and comma

我的目标是得到这样的结果: 150.000,5448.876,05这意味着我的逗号是小数点的起始48.876,05

到目前为止,这是我的代码:

<?php
    //cut numbers after comma if there are any, after 2 digits
    $matchPattern = '/[0-9]+(?:\,[0-9]{2}){0,2}/';
    //remove everything except numbers, commas and dots
    $repl1 = preg_replace("/[^a-zA-Z0-9,.]/", "", $input);
    //let there be a 0 before comma to have values like 0,75
    $repl2 = preg_replace("/^[0]{1}$/", "",$repl1);
    //now i need you here to help me for the expression putting dots after each 3 numbers, until the comma:
    $repl3 = preg_replace("/regexphere$/", ".", $repl2);
    preg_match($matchPattern, $repl3, $matches);
    echo($matches[0]);
?>

我知道 preg_replacing 3 次是愚蠢的,但我不擅长编写正则表达式。 如果你有更好的想法,不要只是分享,还要解释。 我知道一些类型:

http://regexone.com/lesson/0

先感谢您。

- - - - 更新 - - - -

所以,我需要处理0000,45输入等,以0,45和像010101,84投入1,84
当这完成时,我就完成了。

$input = Input::get('userinput');
$repl1 = preg_replace("/[^0-9,.]/", "", $input);
$repl2 = preg_replace("/^0/", "",$repl1);
$repl3 = str_replace(".","",$repl2);
preg_match('/[0-9]+(?:\,[0-9]{2}){0,2}/', $repl3, $matches);
$repl4 = preg_replace('/(\d)(?=(\d{3})+(?!\d))/', '$1.', $matches[0]);
return repl4;

- - 更新 - -

这是我到目前为止所得到的: https : //ideone.com/5qmslB

我只需要删除逗号之前,数字之前的零。

我不确定这是最好的方法,但我希望它会有所帮助。

这是我与假$input一起使用的更新代码:

<?php
$input = "textmdwrhfejhg../,2222233333,34erw.re.ty";
//cut numbers after comma if there are any, after 2 digits
$matchPattern = '/[0-9]+(?:\,[0-9]{2}){0,2}/';
//remove everything except numbers, commas and dots
$repl1 = trim(preg_replace("/[^0-9,.]/", "", $input), ".,");
echo "$repl1" . "\n";
//let there be a 0 before comma to have values like 0,75, remove the 0
$repl2 = preg_replace("/^0/", "",$repl1);
echo "$repl2" . "\n";
//The expression putting dots after each 3 numbers, until the comma:
$repl3 = preg_replace('/(\d)(?=(?:\d{3})+(?!\d))/', '$1.', $repl2);
echo "$repl3" . "\n";

每 3 个数字后加点表达式

(\d)(?=(?:\d{3})+(?!\d))

在这里,您可以看到它是如何工作的 在普通人中,

  • (\\d) - 我们将在替换模式中使用的捕获组,匹配单个数字......
  • (?=(?:\\d{3})+(?!\\d)) - 后跟 3 位数字组。 External (?=...)是检查但不消耗字符的前瞻结构, (?:\\d{3})+是一个非捕获组(不需要将匹配的文本保留在内存中)精确匹配 3 位数字(由于限制量词{3} )1 次或多次(由于+量词),并且(?!\\d)是否定前瞻检查最后匹配的下一个字符之后的下一个字符 3-数字组不是数字。

如果小数点分隔符后有 3 位以上的数字,这将不起作用。 使用正则表达式,我只能想出一种方法来支持小数点后 4 位数字(?<!,)(\\d)(?=(?:\\d{3})+(?!\\d)) 不确定在 PHP 中是否有没有可变宽度后视的通用方法(在这里,我们也需要一个可变宽度前瞻)。 因此,您可能会考虑用逗号分割$repl2值,并且只将第一部分传递给正则表达式。 然后,结合。 像这样的东西:

$spl = split(',', $repl2); // $repl2 is 1234,123456
$repl3 = preg_replace('/(\d)(?=(?:\d{3})+(?!\d))/', '$1.', $spl[0]);
$repl3 .= "," . $spl[1]; // "1.234" + "," + "123456"
echo "$repl3" . "\n";    // 1.234,123456

更新

我想出最终代码

$input = "textmdwrhfejhg../0005456,2222233333,34erw.re.ty";
//Here's mine :
$repl1 = trim(preg_replace("/[^0-9,.]/", "", $input), '.,');
//following line just removes one zero, i want it to remove all chars like
//Input : 000549,569     Output : 549,569
echo "$repl1\n";
$repl2 = preg_replace("/^0+(?!,)/", "",$repl1);
$repl3 = str_replace(".","",$repl2);
preg_match('/[0-9]+(?:\,[0-9]{2}){0,2}/', $repl3, $matches);
$repl4 = preg_replace('/(\d)(?=(\d{3})+(?!\d))/', '$1.', $matches[0]);
echo $repl4;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM