Ответ 1
Правильное регулярное выражение для сопоставления чисел с запятыми и десятичными знаками выглядит следующим образом (первые два будут проверять правильность форматирования номера):
десятичная опция (две десятичные знаки)
^[+-]?[0-9]{1,3}(?:,?[0-9]{3})*(?:\.[0-9]{2})?$
Разъяснения:
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) «$»
Будет соответствовать:
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}$
Разъяснения:
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) «$»
Будет соответствовать:
1,432.01
456.56
654,246.43
324.75
Не будет соответствовать:
1,43,2.01
456,
654,246
324.7523
Соответствует номерам, разделенным запятыми или десятичными знаками без разбора:
^(\d+(.|,))+(\d)+$
Разъяснения:
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
1234,12345.5467.
,125
,.234
123,.1245.
Примечание: оберните либо в группу, а затем просто потяните группу, сообщите мне, если вам нужно больше деталей.
Описание: Этот тип RegEx работает с любым языком (PHP, Python, C, С++, С#, JavaScript, jQuery и т.д.). Эти регулярные выражения хороши для валюты в основном.