At 6/14/12 09:01 PM, EuPlonKa wrote:
My understanding is that a try/catch is pretty expensive, and should be avoided if possible. If a non-scientific notation number checker is all you need then this would be fine.
I'm not very experienced with Java but I'm willing to bet that a regular expression is more expensive than a try/catch block.
I also found another flaw in that regex: it has no support for commas; any numbers with the 1000 decimal places separated by commas will not be parsed properly. It also doesn't seem to support regular decimal places either.
These are the results of me toying with the OP's regex:
1,657,414.155 // Treated as four numbers: "1", "657", "414", and "155".
5\522-5\1 // Treated as a single number. Backslashes and hyphen don't affect it.
+4200 // Treated as "4200"; plus sign ignored.
-4200 // Treated as "-4200"; negative sign recognised.
This is pretty much a shining example of why you shouldn't use regular expressions when you don't understand them.
If the OP wants to learn this is an excellent, albeit incomplete, resource.