There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
fred rosenberger wrote:Why do you want a single regex to do all these things? Wouldn't it be easier to write several, smaller ones that each do a picece of the checks you need?
John Landon wrote:No because I need to do it in validation annotation
Ron McLeod wrote:
John Landon wrote:No because I need to do it in validation annotation
You could make your annotation take multiple expressions.
John Landon wrote:Thanks so how would you put this inside?
Carey Brown wrote:
Haven't tested it but I think this should work.
If you look closely you'll see that it is one long regex String put together with String (+) concatenation.How about leading zeros? Pleas how do I apply them all at once?
Carey Brown wrote:
If you look closely you'll see that it is one long regex String put together with String (+) concatenation.How about leading zeros? Pleas how do I apply them all at once?
I just broke it out so that you can see the separate rules separated by an OR (|).
Henry Wong wrote:
First, I guess I can take a shot at it (and without actually compiling or testing it, so take with a grain of salt)...![]()
Second, assuming that this even works, I highly recommend that you don't use it without actually understanding it !!
Henry
John Landon wrote:
It passes even if you do 75.550 (the extra zero should fail it)
Carey Brown wrote:When working with complex regular expressions I find it useful to create a 20 line regex tester program that takes as parameters an array of "good" test cases that are expected to match, and an array of "bad" test cases that are expected to fail. If any test doesn't result in the expected outcome it should report the failed string.
John Landon wrote:
Carey Brown wrote:When working with complex regular expressions I find it useful to create a 20 line regex tester program that takes as parameters an array of "good" test cases that are expected to match, and an array of "bad" test cases that are expected to fail. If any test doesn't result in the expected outcome it should report the failed string.
It matches the 0
http://www.freeformatter.com/java-regex-tester.html#ad-output
All things are lawful, but not all things are profitable.
John Landon wrote:Also it doesnt match number 5
All things are lawful, but not all things are profitable.
Knute Snortum wrote:
John Landon wrote:Also it doesnt match number 5
Henry Wong's regex? It does for me, running the little program.
John Landon wrote:
This is awesome thanks (the closest I've seen) but it does match 5.0.......
Henry Wong wrote:
John Landon wrote:
This is awesome thanks (the closest I've seen) but it does match 5.0.......
It matches "5.0" because that is how the regex is coded. I took your original post, as disallowing trailing zeros, but allowing one digit after the decimal point. So, "5.00" is not allowed, but "5.0" is allowed.
The fix to this, is actually very simple. Just get rid of the alternation after the decimal, that allows it.
And .... Remember when I said that you should never use a regex that you don't understand? Well, this fix is ridiculously easy to do, and this is a chance for you to understand the regex. So, give it a try at fixing it yourself.
Henry
John Landon wrote:Is there any chance to limit the number of digits total (except the dot) say to 11?
Campbell Ritchie wrote:What is wrong with 5.0? That is a normal format for a number. Similarly 0.5.