简体   繁体   English

php - regex - 如何从字符串中提取带小数(点和逗号)的数字(例如1,120.01)?

[英]php - regex - how to extract a number with decimal (dot and comma) from a string (e.g. 1,120.01)?

how to extract a number with decimal (dot and comma) from a string (eg 1,120.01) ? 如何从字符串中提取带小数(点和逗号)的数字(例如1,120.01)? I have a regex but doesn't seem to play well with commas 我有一个正则表达式,但似乎不能用逗号

preg_match('/([0-9]+\.[0-9]+)/', $s, $matches);

The correct regex for matching numbers with commas and decimals is as follows (The first two will validate that the number is correctly formatted): 使用逗号和小数匹配数字的正确正则表达式如下(前两个将验证数字格式是否正确):


decimal optional (two decimal places) 十进制可选(小数点后两位)

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

正则表达式可视化

Debuggex Demo Debuggex演示

Explained: 解释:

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,3}»
   Between one and 3 times, as many times as possible, giving back as needed (greedy) «{1,3}»
Match 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]{3}»
      Exactly 3 times «{3}»
Match 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]{2}»
      Exactly 2 times «{2}»
Assert position at the end of the string (or before the line break at the end of the string, if any) «$»

Will Match: 将匹配:

1,432.01
456.56
654,246.43
432
321,543

Will not Match 不会匹配

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

decimal mandatory (two decimal places) 十进制强制(小数点后两位)

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

正则表达式可视化

Debuggex Demo Debuggex演示

Explained: 解释:

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,3}»
   Between one and 3 times, as many times as possible, giving back as needed (greedy) «{1,3}»
Match 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]{3}»
      Exactly 3 times «{3}»
Match the character “.” literally «\.»
Match a single character in the range between “0” and “9” «[0-9]{2}»
   Exactly 2 times «{2}»
Assert position at the end of the string (or before the line break at the end of the string, if any) «$»

Will Match: 将匹配:

1,432.01
456.56
654,246.43
324.75

Will Not Match: 不匹配:

1,43,2.01
456,
654,246
324.7523

Matches Numbers separated by commas or decimals indiscriminately: 匹配不加选择地用逗号或小数分隔的数字:

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

正则表达式可视化

Debuggex Demo Debuggex演示

Explained: 解释:

    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»

Will Match: 将匹配:

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

Will Not Match: 不匹配:

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

Note: wrap either in a group and then just pull the group, let me know if you need more specifics. 注意: 在一个组中包装,然后只需拉出组,如果您需要更多细节,请告诉我。

Description: This type of RegEx works with any language really (PHP, Python, C, C++, C#, JavaScript, jQuery, etc). 描述: 这种类型的RegEx可以使用任何语言(PHP,Python,C,C ++,C#,JavaScript,jQuery等)。 These Regular Expressions are good for currency mainly. 这些正则表达式主要适用于货币。

Add the comma to the range that can be in front of the dot: 将逗号添加到可以位于点前面的范围:

/([0-9,]+\.[0-9]+)/
#     ^ Comma

And this regex: 这个正则表达式:

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

Will only match 只会匹配

1,067120.01
121,34,120.01

But not 但不是

,,,.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 `+`
# )/

You can use this regex: - 你可以使用这个正则表达式: -

/((?:[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.
 )/

The locale-aware float (%f) might be used with sscanf. 可识别区域设置的浮点(%f)可能与sscanf一起使用。

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

That doesn't split the parts into an array though. 但是,这并没有将部件拆分成阵列。 It simply parses a float. 它只是解析一个浮点数。

See also: http://php.net/manual/en/function.sprintf.php 另见: http//php.net/manual/en/function.sprintf.php

A regex approach: 正则表达式方法:

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

这应该工作

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

Here is a great working regex. 这是一个伟大的工作正则表达式。 This accepts numbers with commas and decimals. 这接受带逗号和小数的数字。

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

暂无
暂无

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

相关问题 如何提取某些HTML标签,例如 <ul> 在PHP中使用带有preg_match_all的Regex? - How can I extract certain HTML tags e.g. <ul> using Regex with preg_match_all in PHP? php / laravel如何将数字从1转换为2 ^ 31-1转换成十六进制数字,例如包含15位数字的传送时间 - php/laravel how to transform number from 1 to 2^31-1 into hexadecimal number which contains e.g. 15 digits evey time 如何在PHP中循环$ num,例如1、2、3、4、1、2、3、4、1、2、3、4 - How to loop $num in php, e.g. 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 如果未知变量(例如电子邮件)在字符串中,如何在 PHP8 中检查? - How to check in PHP8 if a unknown variable (e.g. e-mail) is in a string? PHP:从列标题中查找电子表格(例如 Excel / LibreOffice Calc)列号 - PHP: Find spreadsheet (e.g. Excel / LibreOffice Calc) column number from column title 如何将PHP时区字符串(例如欧洲/柏林)转换为codeigniter时区(例如UP1) - how to convert PHP timezone string(e.g Europe/Berlin) to codeigniter timezone(e.g. UP1) PHP - 如何在某一点将“...”附加到不同长度的字符串? 例如,200个字符之后 - PHP - How to append “…” to a string of varying length at a certain point? e.g. After 200 chars 如何在PHP中使用Ajax从网站(例如IMDB)中获取数据 - How can I fetch data from a website (e.g. IMDB ) using Ajax in PHP 如何从今天的日期(PHP)计算并获取过去的日期(例如3周前) - How to calculate and get a date in the past (e.g. 3 weeks ago) from today's date (PHP) 用点 (.) 替换逗号 (,) 正则表达式 php - Replace Comma(,) with Dot(.) RegEx php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM