The first rule of optimisation is "Make it work first". Your regex does not even compile, the closing '/' being missing.
Once you have fixed that, it still will exlude 9, 10, 99 etc. and allow 99.999, 99.9999 etc.
Once you made the grouping conditional, and anchored the end of the regex, it will probably work, and you could probably save a few microseconds by making the grouping non-capturing.
But really--how many times are you going to run this test that you need to optimise it?
Which then makes me wonder what question you are really asking?
- Is it, "How can I write a condition to 'not allow more than 100 and only two decimal places'"?
- Or maybe, "How can I combine the two parts of this conditional into one"?
If the former, ask that question--and forget the "optimize" imperative.
If the latter, it is certainly possible, but it would probably run more slowly that the two-part test.
Examine what is said, not who speaks.
Silence betokens consent.
Love the truth but pardon error.
|