php-正则表达式-如何从字符串(例如1,120.01)中提取带小数的数字(点和逗号)? [英] php - regex - how to extract a number with decimal (dot and comma) from a string (e.g. 1,120.01)?

查看:102
本文介绍了php-正则表达式-如何从字符串(例如1,120.01)中提取带小数的数字(点和逗号)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从字符串(例如1,120.01)中提取带小数的数字(点和逗号)? 我有一个正则表达式,但似乎不能很好地使用逗号

how to extract a number with decimal (dot and comma) from a string (e.g. 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演示

Debuggex Demo

说明:

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


十进制强制性(两位小数)

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

Debuggex演示

Debuggex Demo

说明:

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


匹配数字,用逗号或小数点任意分隔:

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

Debuggex演示

Debuggex Demo

说明:

    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.

描述:这种RegEx实际上可以与任何语言(PHP,Python,C,C ++,C#,JavaScript,jQuery等)一起使用.这些正则表达式主要适用于货币.

Description: This type of RegEx works with any language really (PHP, Python, C, C++, C#, JavaScript, jQuery, etc). These Regular Expressions are good for currency mainly.

这篇关于php-正则表达式-如何从字符串(例如1,120.01)中提取带小数的数字(点和逗号)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆