regex for multiple formats decimals and thousand separator

Ali Samii

I need regex to validate a number that could contain thousand seperators that would be one of three different characters (. , or ) or decimals that could be either , or .. There are no max or min values, so that isn't a concern. If a decimal isn't in the number, it should be added. Once detected, I would then be able to process the resulting expressions into a set standard format.

Example values that would need to be detected (values on the left are what will be detected, values on the right will be the output we will code to, but displayed here for the benefit of better a understanding of my needs):

11,111.11 => 11111.11

11'111.11 => 11111.11

11 111.11 => 11111.11

11.111,11 => 11111.11

11'111,11 => 11111.11

11 111,11 => 11111.11

11,111 => 11111.00

11'111 => 11111.00

11 111 => 11111.00

11.111 => 11111.00

This is needed for a web site where the input values could be different based on different standard formats (depending on the end-user). In Switzerland, all those potential combinations may be used.

Help would be appreciated.

hex494D49

You may try the snippet below since I don't see how to achieve this using a singe regular expression.

$string = array("0,89", "11,111,111,111", "11,111.11", "11'111.11", "11 111.11", "11.111,11", "11'111,11", "11 111,11", "11,111", "11'111", "11 111", "11.111");
$pattern = "/^(\d{1,3}([,\s.']\d{3})*|\d+)([.,]\d+)?$/";
foreach($string as $price){
    preg_match($pattern, $price, $matches);
    echo $matches[0] , " -> " , preg_replace("/[,\s.']/", "", $matches[1]) , ((!empty($matches[3])) ? preg_replace("/[,\s.']/", ".", $matches[3]) : ".00") , "<br />";
}

Output:

0,89 -> 0.89
11,111,111,111 -> 11111111111.00
11,111.11 -> 11111.11
11'111.11 -> 11111.11
11 111.11 -> 11111.11
11.111,11 -> 11111.11
11'111,11 -> 11111.11
11 111,11 -> 11111.11
11,111 -> 11111.00
11'111 -> 11111.00
11 111 -> 11111.00
11.111 -> 11111.00

As you can see, in the first step, the pattern above will match any (valid) price format and place it into three groups. In the second step, the group (1) will be cleaned up of any separators while the separator in group (3) if exists, would be replaced with a dot (.) (and the zeros will be added if needed)

For more testings check the demo

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Format integer number with . as thousand separator and no decimals

From Dev

Regex valid numbers with thousand separator

From Dev

Regex valid numbers with thousand separator

From Dev

JavaScript regex to accept numbers with comma separator (thousands) and dot separator (decimals)

From Dev

Regex for positive lookahead thousand separator that would not mach numbers after dot

From Dev

Regex for 8,2 decimal with thousand separator in JAVA

From Dev

Set standard number format with thousand separator, 2 decimals and minus sign for negative numbers using VBA?

From Dev

Python regex for number with or without decimals using a dot or comma as separator?

From Dev

Thousand Separator in DBGrid

From Dev

Devexpress XRtableCell thousand separator?

From Dev

Thousand separator in awk

From Dev

Multiple Regex Pattern with different formats

From Dev

Python regex: Subsentence that contains number which can have thousand separator and decimal

From Dev

validate decimal with decimal separator and thousand separator

From Dev

How to format number with "." as thousand separator, and "," as decimal separator?

From Dev

Set global thousand separator on knitr

From Dev

Format number with space as thousand separator

From Dev

Thousand separator not working right way

From Dev

Adding Thousand Separator to Int in Swift

From Dev

Remove thousand separator from string

From Dev

dot instead of comma as thousand separator

From Dev

Thousand separator with wide/unicode stream

From Dev

Thousand separator not working right way

From Dev

Adding thousand separator to jQuery Counter

From Dev

Format number with space as thousand separator

From Dev

Adding Thousand Separator Automatically (Swift)

From Dev

String Number format with "/" for thousand separator

From Dev

Regex for Decimals

From Dev

Regex for Decimals

Related Related

  1. 1

    Format integer number with . as thousand separator and no decimals

  2. 2

    Regex valid numbers with thousand separator

  3. 3

    Regex valid numbers with thousand separator

  4. 4

    JavaScript regex to accept numbers with comma separator (thousands) and dot separator (decimals)

  5. 5

    Regex for positive lookahead thousand separator that would not mach numbers after dot

  6. 6

    Regex for 8,2 decimal with thousand separator in JAVA

  7. 7

    Set standard number format with thousand separator, 2 decimals and minus sign for negative numbers using VBA?

  8. 8

    Python regex for number with or without decimals using a dot or comma as separator?

  9. 9

    Thousand Separator in DBGrid

  10. 10

    Devexpress XRtableCell thousand separator?

  11. 11

    Thousand separator in awk

  12. 12

    Multiple Regex Pattern with different formats

  13. 13

    Python regex: Subsentence that contains number which can have thousand separator and decimal

  14. 14

    validate decimal with decimal separator and thousand separator

  15. 15

    How to format number with "." as thousand separator, and "," as decimal separator?

  16. 16

    Set global thousand separator on knitr

  17. 17

    Format number with space as thousand separator

  18. 18

    Thousand separator not working right way

  19. 19

    Adding Thousand Separator to Int in Swift

  20. 20

    Remove thousand separator from string

  21. 21

    dot instead of comma as thousand separator

  22. 22

    Thousand separator with wide/unicode stream

  23. 23

    Thousand separator not working right way

  24. 24

    Adding thousand separator to jQuery Counter

  25. 25

    Format number with space as thousand separator

  26. 26

    Adding Thousand Separator Automatically (Swift)

  27. 27

    String Number format with "/" for thousand separator

  28. 28

    Regex for Decimals

  29. 29

    Regex for Decimals

HotTag

Archive