Win a copy of The Journey To Enterprise Agility this week in the Agile and Other Processes forum! And see the welcome thread for 20% off.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

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/
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!