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

时间:2022-03-30 16:40:29

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

如何从字符串(例如1,120.01)中提取一个小数(点和逗号)?我有一个regex,但似乎不太适合使用逗号

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

6 个解决方案

#1


25  

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})?$

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

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
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}$

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

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
324.75

Will Not Match:

将不匹配:

1,43,2.01
456,
654,246
324.7523

Matches Numbers separated by commas or decimals indiscriminately:

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

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

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
1234,12345.5467.
,125
,.234
123,.1245.

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). These Regular Expressions are good for currency mainly.

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

#2


3  

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

#3


2  

You can use this regex: -

你可以使用这个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.
 )/

#4


0  

The locale-aware float (%f) might be used with sscanf.

可以在sscanf中使用本地感知浮动(%f)。

$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]+)/

#5


0  

This should work

这应该工作

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

#6


0  

Here is a great working regex. This accepts numbers with commas and decimals.

这里有一个很棒的regex。这个接受数字的逗号和小数。

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

#1


25  

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})?$

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

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
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}$

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

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
324.75

Will Not Match:

将不匹配:

1,43,2.01
456,
654,246
324.7523

Matches Numbers separated by commas or decimals indiscriminately:

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

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

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
1234,12345.5467.
,125
,.234
123,.1245.

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). These Regular Expressions are good for currency mainly.

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

#2


3  

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

#3


2  

You can use this regex: -

你可以使用这个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.
 )/

#4


0  

The locale-aware float (%f) might be used with sscanf.

可以在sscanf中使用本地感知浮动(%f)。

$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]+)/

#5


0  

This should work

这应该工作

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

#6


0  

Here is a great working regex. This accepts numbers with commas and decimals.

这里有一个很棒的regex。这个接受数字的逗号和小数。

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