This week's book giveaway is in the OCAJP forum.
We're giving away four copies of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) and have Khalid A Mughal & Rolf W Rasmussen on-line!
See this thread for details.
Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

regex - only allow numbers & one decimal

 
Ron Landreth
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to write a regular expression that will only allow numbers and one decimal place (aka dollars and cents). How would I write such an expression?

Thanks in advance.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What have you tried so far?

(See Tutorial - Regular Expressions.)
 
David McCombs
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you looking to do this with the java API or in a more general way?

To write it out on paper it would look something like this:

(0+1+2...+9)*.(0+1+2...+9)(0+1+2...+9), where + is or, * means repeat 0 or more times and the 4 expressions are concatanated.

I don't know if this will be helpful to you, but it does describe what you are looking to do. To convert it into a Java regex, reading the link that Marc supplied should get your problem solved.
 
Nav Letha
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to escape"." like "\." else, it will take any single character for decimal.
 
David McCombs
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The regex I posted was not implementation specific.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic