• 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:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

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/
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!