Jump to content

[RegExp] Financial value yes/no

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
Fischerspooner

Fischerspooner

    Newbie

  • Members
  • PipPip
  • 27 posts
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.

#2
Lop

Lop

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,172 posts
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]+ 


#3
Fischerspooner

Fischerspooner

    Newbie

  • Members
  • PipPip
  • 27 posts

Lop said:

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.

#4
Lop

Lop

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,172 posts
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]

#5
Fischerspooner

Fischerspooner

    Newbie

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