Win a copy of Escape Velocity: Better Metrics for Agile Teams this week in the Agile and Other Processes forum!
  • 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Frank Carver
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Al Hobbs
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • fred rosenberger

Regx problem

 
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I am working on a regx problem. I already have a regx like this [0-9]*([.][0-9]{2})*. This is a validation for amount. Now with this I want to check if any given amount is 0 then it will be cought. For an example 10 is valid but 0 is invalid.

Example
Valid amount format
10
10.00
1.00
1

Invalid
0
Would be really appreciated if someone help me on this.

Thanks in advance
Kaushik
 
Greenhorn
Posts: 27
Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Only 0 is not allowed?, maybe you can negate only that with [^0$]
 
Kaushik Baral
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Could you please tell me how to use this with the existing regex I have
 
Marshal
Posts: 76468
366
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can search for regular expression resources. We have a link here. You may be able to find a ready‑made regex.
 
Saloon Keeper
Posts: 14325
321
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You should do this by specifying the input should contain one character [1-9], and then zero or more characters [0-9]. By the way, it doesn't make sense that you have an extra quantifier at the end of your regex, it will only repeat the fractional part. For zero or one, use '?', not '*'.
 
Sheriff
Posts: 7113
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:You should do this by specifying the input should contain one character [1-9], and then zero or more characters [0-9].


The only problem with this that I can think of is that it would miss pennies, like the amount 0.23.

I think this is not possible or too hard for a regex. Do your regex and then add an if statement for the zero amount.
 
We noticed he had no friends. So we gave him this tiny ad:
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic