• Post Reply Bookmark Topic Watch Topic
  • New Topic

Regex help  RSS feed

 
John Landon
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
I need regex for:
String must fail if:
1) Non numeric
2) If any leading zeroes are entered without a decimal point
3) If there are any zeroes trailing a decimal point
4) If there is a decimal point with no numbers before it
5) If there is decimal point more then 10 digits after the zero

I came up with this but its not quite right it still matches if there are zeroes
^\d+(\.\d{1,10})?$

Thanks!
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Go through the Scanner class documentation and the Pattern class. You might be lucky and find regexes for numbers ready made there. Is no 5 written correctly? Do you mean not more than ten digits after the decimal point?
Of course \d will match 0. You would have to try [1-9] or something like that.
You do realise that your specification will fail for this valid double literal: 123.0.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

No because I need to do it in validation annotation
 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Landon wrote:No because I need to do it in validation annotation

You could make your annotation take multiple expressions.
 
John Landon
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ron McLeod wrote:
John Landon wrote:No because I need to do it in validation annotation

You could make your annotation take multiple expressions.


Thanks so how would you put this inside? (its regex that does what I want except trailing zeroes):^\d+(\.\d{1,10})?$
 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So...

by #2,
this (leading zeroes without a decimal) would fail: 0053
and this (leading zeroes with a decimal) would pass: 0053.

But by #3, this would fail: 0053.00

I don't get what those rules with leading and trailing zeroes are going for.

 
Ron McLeod
Bartender
Posts: 1602
232
Android Angular Framework Eclipse IDE Java Linux MySQL Database Redhat TypeScript
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Landon wrote:Thanks so how would you put this inside?

Are you asking how to put multiple expression in the annotation?
 
John Landon
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
05.5 will fail
5.50 will fail
0.5 will succeed
 
Carey Brown
Saloon Keeper
Posts: 3311
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Haven't tested it but I think this should work.
 
John Landon
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
Haven't tested it but I think this should work.


How about leading zeros? Pleas how do I apply them all at once?
 
Carey Brown
Saloon Keeper
Posts: 3311
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How about leading zeros? Pleas how do I apply them all at once?
If you look closely you'll see that it is one long regex String put together with String (+) concatenation.

I just broke it out so that you can see the separate rules separated by an OR (|).
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Carey Brown wrote:
How about leading zeros? Pleas how do I apply them all at once?
If you look closely you'll see that it is one long regex String put together with String (+) concatenation.

I just broke it out so that you can see the separate rules separated by an OR (|).


thanks.
Yeas I saw but this website denies it
http://www.freeformatter.com/java-regex-tester.html
 
John Landon
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


It doesnt. Does the same thing as mine: ^\d+(\.\d{1,10})?$

It passes even if you do 75.550 (the extra zero should fail it)
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Landon wrote:
It passes even if you do 75.550 (the extra zero should fail it)


The test failed, as expected, when I tried it...



It printed "false", as expected.

Henry
 
Carey Brown
Saloon Keeper
Posts: 3311
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
John Landon
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


Also it doesnt match number 5
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I like to break long regexes into chunks and comment them.  I think it makes working with them easier and it certainly helps anyone maintaining the code, if only to state my intent.

I can see from this that the regex will fail to match "75.50", which I think it should.  Edit: I guess not, looking at the specs.
 
Knute Snortum
Sheriff
Posts: 4276
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Landon wrote:Also it doesnt match number 5

Henry Wong's regex?  It does for me, running the little program.
 
John Landon
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Knute Snortum wrote:
John Landon wrote:Also it doesnt match number 5

Henry Wong's regex?  It does for me, running the little program.


This is awesome thanks (the closest I've seen) but it does match 5.0.......
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Ranch Hand
Posts: 239
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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


LOL Thanks Henry. I removed //+     "0"        // Match zero and it works. Is there any chance to limit the number of digits total (except the dot) say to 11?
 
Stephan van Hulst
Saloon Keeper
Posts: 7973
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, not without a regex that's much more complex.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
John Landon wrote:Is there any chance to limit the number of digits total (except the dot) say to 11?


Sure. You can do something like using a negative look-ahead, at the beginning of the regex -- to match, 12 or more digits til the end-of-input, which will, of course, cause the regex to fail, when it encounters more than 11 digits.

Now, if what I said sounds like gibberish ... ... it is likely because this is an advanced feature. At this point, I am not even convinced that you understand the regex that you currently have (that we did for you)....

Anyway, I highly recommend doing a little studying of regular expressions, to fully understand the regex that you are currently using. And once you feel comfortable with that, then do a little research on look-aheads. This should lead you to the solution that I described.

Hope this helps,
Henry
 
Campbell Ritchie
Marshal
Posts: 56534
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is wrong with 5.0? That is a normal format for a number. Similarly 0.5.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:What is wrong with 5.0? That is a normal format for a number. Similarly 0.5.


Agreed. And my regular expression allowed for both!

Henry
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!