0
How can I create a regex (which will be used with preg_match
) to the following format 1.123,12
?
0
How can I create a regex (which will be used with preg_match
) to the following format 1.123,12
?
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:
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 php regex
You are not signed in. Login or sign up in order to post.
What are the variations of the number? It has maximum or minimum limit, from 0 to million, billion...?
– Daniel Dutra
It’s 0 to a million
– Thepeter
That number comes from where?
– rray
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 .
– Thepeter
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.– rray
yes more I didn’t want this to be doing this to every record because it can also come strings
– Thepeter
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.
– utluiz