如何从字符串中提取带有小数点和逗号的数字(例如1,120.01)?使用PHP正则表达式。

8

如何从字符串中提取带小数点和逗号的数字(例如1,120.01)?我有一个正则表达式,但与逗号似乎不兼容。

preg_match('/([0-9]+\.[0-9]+)/', $s, $matches);
提示:如果您的字符串中包含逗号,则需要先将其替换为小数点,然后再使用上述代码提取数字。
6个回答

30

使用正确的正则表达式来匹配带逗号和小数点的数字,如下所示(前两个将验证数字的格式是否正确):


可选小数点(两位小数)

^[+-]?[0-9]{1,3}(?:,?[0-9]{3})*(?:\.[0-9]{2})?$

正则表达式可视化

Debuggex演示

解释:

number (decimal optional)

^[+-]?[0-9]{1,3}(?:,?[0-9]{3})*(?:\.[0-9]{2})?$

Options: case insensitive

Assert position at the beginning of the string «^»
Match a single character present in the list below «[+-]?»
   Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
   The character “+” «+»
   The character “-” «-»
Match a single character in the range between “0” and “9” «[0-9]{1,3Between one and 3 times, as many times as possible, giving back as needed (greedy) «{1,3Match the regular expression below «(?:,?[0-9]{3})*»
   Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
   Match the character “,” literally «,?»
      Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
   Match a single character in the range between “0” and “9” «[0-9]{3Exactly 3 times «{3Match the regular expression below «(?:\.[0-9]{2})?»
   Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
   Match the character “.” literally «\.»
   Match a single character in the range between “0” and “9” «[0-9]{2Exactly 2 times «{2Assert position at the end of the string (or before the line break at the end of the string, if any) «$»

将匹配:

1,432.01
456.56
654,246.43
432
321,543

不会匹配

454325234.31
324,123.432
,,,312,.32
123,.23

强制保留两位小数

^[+-]?[0-9]{1,3}(?:,?[0-9]{3})*\.[0-9]{2}$

正则表达式可视化

Debuggex演示

解释:

number (decimal required)

^[+-]?[0-9]{1,3}(?:,?[0-9]{3})*\.[0-9]{2}$

Options: case insensitive

Assert position at the beginning of the string «^»
Match a single character present in the list below «[+-]?»
   Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
   The character “+” «+»
   The character “-” «-»
Match a single character in the range between “0” and “9” «[0-9]{1,3Between one and 3 times, as many times as possible, giving back as needed (greedy) «{1,3Match the regular expression below «(?:,?[0-9]{3})*»
   Between zero and unlimited times, as many times as possible, giving back as needed (greedy) «*»
   Match the character “,” literally «,?»
      Between zero and one times, as many times as possible, giving back as needed (greedy) «?»
   Match a single character in the range between “0” and “9” «[0-9]{3Exactly 3 times «{3Match the character “.” literally «\.»
Match a single character in the range between “0” and “9” «[0-9]{2Exactly 2 times «{2Assert position at the end of the string (or before the line break at the end of the string, if any) «$»

匹配:

1,432.01
456.56
654,246.43
324.75

不匹配:

1,43,2.01
456,
654,246
324.7523
匹配描述中数字,不区分逗号或小数点的分隔:
^(\d+(.|,))+(\d)+$

正则表达式可视化

Debuggex演示

解释:

    Matches Numbers Separated by , or .

^(\d+(.|,))+(\d)+$

Options: case insensitive

Match the regular expression below and capture its match into backreference number 1 «(\d+(.|,))+»
   Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
   Note: You repeated the capturing group itself.  The group will capture only the last iteration.  Put a capturing group around the repeated group to capture all iterations. «+»
   Match a single digit 0..9 «\d+»
      Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
   Match the regular expression below and capture its match into backreference number 2 «(.|,)»
      Match either the regular expression below (attempting the next alternative only if this one fails) «.»
         Match any single character that is not a line break character «.»
      Or match regular expression number 2 below (the entire group fails if this one fails to match) «,»
         Match the character “,” literally «,»
Match the regular expression below and capture its match into backreference number 3 «(\d)+»
   Between one and unlimited times, as many times as possible, giving back as needed (greedy) «+»
   Note: You repeated the capturing group itself.  The group will capture only the last iteration.  Put a capturing group around the repeated group to capture all iterations. «+»
   Match a single digit 0..9 «\d»

匹配成功:

1,32.543,2
5456.35,3.2,6.1
2,7
1.6

不会匹配:

1,.2 // two ., side by side
1234,12345.5467. // ends in a .
,125 // begins in a ,
,.234 // begins in a , and two symbols side by side
123,.1245. // ends in a . and two symbols side by side

注意: 把要匹配的内容用括号包起来,然后通过提取括号组来获取匹配结果,如果需要更具体的说明,请告诉我。

描述:这种类型的正则表达式实际上适用于任何语言(PHP、Python、C、C++、C#、JavaScript、jQuery等)。这些正则表达式主要用于货币方面。


5
你可以使用这个正则表达式:-
/((?:[0-9]+,)*[0-9]+(?:\.[0-9]+)?)/

Explanation: -
解释:-
/(
    (?:[0-9]+,)*   # Match 1 or more repetition of digit followed by a `comma`. 
                   # Zero or more repetition of the above pattern.
    [0-9]+         # Match one or more digits before `.`
    (?:            # A non-capturing group
        \.         # A dot
        [0-9]+     # Digits after `.`
    )?             # Make the fractional part optional.
 )/

1
那么44.336.455,33怎么样? - Anirudha
@Some1.Kill.The.DJ. 没有考虑到那个问题,因为没有具体说明。这将需要一些修改。 - Rohit Jain
@RohitJain 我没有给你的回答点踩,但我认为你的正则表达式会接受 192,192,1,.2。因为小数点前面总是有一个逗号。 - Michael
@RohitJain 现在您不能接受不包含逗号的数字。 - Michael
@Michael.. 哦,好的发现。 :) 已编辑。 - Rohit Jain
您IP地址为143.198.54.68,由于运营成本限制,当前对于免费用户的使用频率限制为每个IP每72小时10次对话,如需解除限制,请点击左下角设置图标按钮(手机用户先点击左上角菜单按钮)。 - mroncetwice

4
在点号前面的范围中添加逗号:
/([0-9,]+\.[0-9]+)/
#     ^ Comma

而这个正则表达式:

/((?:\d,?)+\d\.[0-9]*)/

只匹配

1,067120.01
121,34,120.01

但不是

,,,.01
,,1,.01
12,,,.01

# /(
#   (?:\d,?) Matches a Digit followed by a optional comma
#   +        And at least one or more of the previous
#   \d       Followed by a digit (To prevent it from matching `1234,.123`)
#   \.?      Followed by a (optional) dot
#            in case a fraction is mandatory, remove the `?` in the previous section.
#   [0-9]*   Followed by any number of digits  -->  fraction? replace the `*` with a `+`
# )/

@Michael:没错,正在处理中。 - Cerbrus
@Some1.Kill.The.DJ:说得好。(或者应该说是没有说什么:P)正在编辑。 - Cerbrus
@user1963938:你的输入是什么? - Cerbrus
货币ID和不同格式的数字,例如123;123.00;1,992.00;293,993.00。重点是仅获取数字/值,而不包括货币ID或符号。 - user1963938
@user1963938:我的最新编辑(/((?:\d,?)+\d\.[0-9]*)/)可以匹配完整数字,例如在$US 44,443.00中可以匹配到44,443.00 - Cerbrus
显示剩余3条评论

0

可以使用具有区域设置的浮点数(%f)与sscanf一起使用。

$result = sscanf($s, '%f')

这并没有将部分拆分成数组。它只是解析了一个浮点数。

另请参阅:http://php.net/manual/en/function.sprintf.php

正则表达式方法:

/([0-9]{1,3}(?:,[0-9]{3})*\.[0-9]+)/

0

这应该可以工作

preg_match('/\d{1,3}(,\d{3})*(\.\d+)?/', $s, $matches);

2131,.13123,123,1234 怎么样? - Cerbrus
哦,我的错,抱歉我太专注于他的例子了,如果我有正确的正则表达式,我会编辑它,如果没有,我会删除它,谢谢@Cerbrus。 - Abu Romaïssae
ن¾‹ه¦‚,è؟™ن¹ںهŒ¹é…چ,,,.01,2131,.13ه’Œ123,123.123,123.123م€‚ - Cerbrus
现在我猜这应该只捕获有效的数字,如1,234.56。 - Abu Romaïssae

0

这是一个非常有效的正则表达式。它可以接受带有逗号和小数点的数字。

/^-?(?:\d+|\d{1,3}(?:,\d{3})+)?(?:\.\d+)?$/

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接