Win a copy of Classic Computer Science Problems in Swift this week in the iOS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Regexp to validate 'amount' numbers  RSS feed

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

For a program that I am writing, I need to validate a number passed in, in the form of a String.

The rule is: Eliminate all characters other than digits (0-9) and a period (.).

For example:
If you pass in "2500", it should return "2500".
If you pass in "2500.00", it should return "2500.00".
If you pass in "2,500.00", it should return "2500.00".
If you pass in "2,500.00", it should return "2500.00".
If you pass in "2,5##00.00", it should return "2500.00".

I am trying to find a regexp that expressed the condition above of "digit or period". Is "\D\." correct? I plan on using this with String.replaceAll method.
 
Ranch Hand
Posts: 781
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you have to tighten your rules. Based on your definitions then

asoifawqnaksjshf1iofh0ihkjvfh*ieriidsfui*+.aiofauhfkjds9fkju1lfkjd3

would be a valid number.
 
Ranch Hand
Posts: 262
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You want to remove anything that's not a digit or a period; you can express that directly with a negated character class:


http://www.regular-expressions.info/
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!