匹配货币格式中的任何逗号和点,但不是最后一个

Match any comma and dot in currency format, but not the last one

我想将货币格式转换为十进制,以十进制数据类型存储在数据库中

示例:

12,345.45 to 12345.45
10.000.00 to 10000.00

我试过这个正则表达式模式/(,|\.)/但是最后一个点(表示美分值)仍然存在,我对如何排除最后一个点感到困惑

预期结果,排除给定字符串中的最后一个点

使用php,你可以这样使用preg_replace

$repl = preg_replace('/\.(?=\d+\.)|,/', '', $str);

RegEx Demo

正则表达式详细信息:

  • \.:匹配.
  • (?=\d+\.):正面前瞻断言我们在 1+ 位数字后有一个点
  • |: 或
  • ,:匹配逗号