Regex for number format

Asked

Viewed 350 times

0

How can I create a regex (which will be used with preg_match) to the following format 1.123,12?

  • What are the variations of the number? It has maximum or minimum limit, from 0 to million, billion...?

  • It’s 0 to a million

  • That number comes from where?

  • comes from a csv file and when importing to a database is only 1.12 if you can verify which are the cases where this happens I can do the number Numberformatter .

  • The best is to use the NumberFormatter nor need regex, remember that the point is the decimal separator so your number gets wrong, the right one would be: 1123.12, you can initially try to change the point for nothing and the comma for point.

  • yes more I didn’t want this to be doing this to every record because it can also come strings

  • I don’t think regex is suitable for numerical validation. Ideally, as @rray said, use a special class or function for this. Do this selectively for the fields you know are numeric. That must be indicated somewhere in the code, right? If there is any problem in the value, just treat the exception or return value accordingly.

Show 2 more comments

1 answer

1

Pattern to get numbers from 0 to 9.999.999.99: "((?:\d\.)?(?:\d{1,3}\.)?\d{1,3},\d{1,2})|(\d)"

The Pattern above gets numbers in the following formats:

  • 0
  • 0,00
  • 00,00
  • 0,000,00
  • 000,000.00
  • 0.000.000,00

Example in Regex101.

I believe there must be better ways to resolve this without regex, but I know little of php.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.