Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

[RegExp] Financial value yes/no


  • Please log in to reply
4 replies to this topic

#1 Fischerspooner

Fischerspooner

    CC Regular

  • Just Joined
  • PipPipPip
  • 27 posts

Posted 12 December 2006 - 01:33 AM

Hi,

I'm trying to determine of a given value is a financial one.

It should match:
- can be positive or negative (ie. 1234.56€ or -1234.56€)
- has minimum 2 digits after the . (ie 1234.5€ should return false)
- whitespaces should be ignored so the user can type 1234.56 € and it still returns true.

I came up with this:
(-|?)[0-9]*(\.|\,)\d\d+€

but I'm having some problems with the whitespaces. Can someone push me in the right direction please? Or maybe it can made better? It's tested with Regular Expression Library.
  • 0

#2 Lop

Lop

    Speaks fluent binary

  • Just Joined
  • PipPipPipPipPipPipPip
  • 1112 posts

Posted 12 December 2006 - 05:54 AM

Each language has slightly different regular expressions but this one matches optional + or -, numbers, optional dot, optional any amount of numbers:

[-+]?[0-9]*\.?[0-9]+ 

  • 0

#3 Fischerspooner

Fischerspooner

    CC Regular

  • Just Joined
  • PipPipPip
  • 27 posts

Posted 12 December 2006 - 06:03 AM

Each language has slightly different regular expressions but this one matches optional + or -, numbers, optional dot, optional any amount of numbers:

[-+]?[0-9]*\.?[0-9]+ 


Thank you :)

Is \d\d the way to ensure there are at least 2 digits? I've tested it with a long series of values and it gives me the desired result. Is there another way to do this?

Oh, the language is Java.
  • 0

#4 Lop

Lop

    Speaks fluent binary

  • Just Joined
  • PipPipPipPipPipPipPip
  • 1112 posts

Posted 12 December 2006 - 06:18 AM

I think you can just do this for two values:

[-+]?[0-9]*\.?[0-9][0-9]

All I did was remove the + and add another [0-9]
  • 0

#5 Fischerspooner

Fischerspooner

    CC Regular

  • Just Joined
  • PipPipPip
  • 27 posts

Posted 12 December 2006 - 11:10 AM

You can replace [0-9] by \d, which means digit.
So I modified my code to
-?\d+(\.|\,)\d\d+€

  • 0




Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download