php – regex – ¿cómo extraer un número decimal (punto y coma) de una cadena (ej. 1,120.01)?

cómo extraer un número decimal (punto y coma) de una cadena (ej. 1,120.01)? Tengo una expresión regular, pero no parece jugar bien con las comas

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

Solutions Collecting From Web of "php – regex – ¿cómo extraer un número decimal (punto y coma) de una cadena (ej. 1,120.01)?"

La expresión regular correcta para hacer coincidir números con comas y decimales es la siguiente (los dos primeros validarán que el número tenga el formato correcto):


decimal opcional (dos decimales)

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

Visualización de expresión regular

Demostración de Debuggex

Explicado:

 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) «$» 

Coincidirá:

 1,432.01 456.56 654,246.43 432 321,543 

No coincidirá

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

decimal obligatorio (dos decimales)

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

Visualización de expresión regular

Demostración de Debuggex

Explicado:

 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) «$» 

Coincidirá:

 1,432.01 456.56 654,246.43 324.75 

No coincidirá:

 1,43,2.01 456, 654,246 324.7523 

Coincide con los números separados por comas o decimales indiscriminadamente:

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

Visualización de expresión regular

Demostración de Debuggex

Explicado:

  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» 

Coincidirá:

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

No coincidirá:

 1,.2 1234,12345.5467. ,125 ,.234 123,.1245. 

Nota: envuelva en un grupo y luego simplemente retire el grupo, avíseme si necesita más detalles.

Descripción: este tipo de RegEx funciona realmente con cualquier lenguaje (PHP, Python, C, C ++, C #, JavaScript, jQuery, etc.). Estas expresiones regulares son buenas para la moneda principalmente.

Agregue la coma al rango que puede estar delante del punto:

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

Y esta expresión regular:

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

Solo coincidirá

 1,067120.01 121,34,120.01 

Pero no

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

Puedes usar esta expresión regular: –

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

Explicación:

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

El flotador con reconocimiento de locale (% f) se puede usar con sscanf.

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

Sin embargo, eso no divide las partes en una matriz. Simplemente analiza un flotador.

Ver también: http://php.net/manual/en/function.sprintf.php

Un enfoque regex:

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

Esto debería funcionar

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

Aquí hay una gran expresión regular de trabajo. Esto acepta números con comas y decimales.

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