I need a regex to get numeric values that can be
111.111,11
111,111.11
111,111
And separate the integer and decimal portions so I can store in a DB with the correct syntax
I tried ([0-9]{1,3}[,.]?)+([,.][0-9]{2})? With no success since it doesn’t detect the second part 🙁
The result should look like:
111.111,11 -> $1 = 111111; $2 = 11
First Answer:
This matches
#,###,##0.00:And this matches
#.###.##0,00:Joining the two (there are smarter/shorter ways to write it, but it works):
You can also, add a capturing group to the last comma (or dot) to check which one was used.
Second Answer:
As pointed by Alan M, my previous solution could fail to reject a value like
11,111111.00where a comma is missing, but the other isn’t. After some tests I reached the following regex that avoids this problem:This deserves some explanation:
^[+-]?[0-9]{1,3}matches the first (1 to 3) digits;(?:(?<comma>\,?)[0-9]{3})?matches on optional comma followed by more 3 digits, and captures the comma (or the inexistence of one) in a group called ‘comma’;(?:\k<comma>[0-9]{3})*matches zero-to-any repetitions of the comma used before (if any) followed by 3 digits;(?:\.[0-9]{2})?$matches optional “cents” at the end of the string.Of course, that will only cover
#,###,##0.00(not#.###.##0,00), but you can always join the regexes like I did above.Final Answer:
Now, a complete solution. Indentations and line breaks are there for readability only.
And this variation captures the separators used:
edit 1: “cents” are now optional;
edit 2: text added;
edit 3: second solution added;
edit 4: complete solution added;
edit 5: headings added;
edit 6: capturing added;
edit 7: last answer broke in two versions;