iAm.Hassan
iAm.Hassan

Reputation: 53

Regular Expression For EURO Format

I want the EURO format to be accepted only in the following forms.

Three digits with "." Be segregated.

i found this expression but not work in my way:

^(0|(([1-9]{1}|[1-9]{1}[0-9]{1}|[1-9]{1}[0-9]{2}){1}(\ [0-9]{3}){0,})),(([0-9]{2})|\-\-)([\ ]{1})(€|EUR|EURO){1}$

I want it to be exactly like the following:

1.123,45

1,23

1.234.567,45

0,56

I want two digit cent to be mandatory.

thanks

Upvotes: 0

Views: 1541

Answers (2)

Hao Wu
Hao Wu

Reputation: 20764

You can try

^(?!0\d)\d{1,3}(?:\.\d{3})*,\d{2}$
  • ^ start of the string
  • (?!0\d) negative lookahead, make sure there's no leading 0s like 01,25, remove it if it is allowed
  • \d{1,3} 1 to 3 digits
  • (?:\.\d{3})* followed by any occurances of texts like .123
  • ,\d{2} followed by 2 decimal digits, mandatory. If you want it to be optional, replace it with (?:,\d{2})?
  • $ end of the string

See the test cases

Upvotes: 2

coladict
coladict

Reputation: 5095

-?(\d{1,3})(\.\d{3})*(,\d{2})? (€|EURO?)

This will capture the numbers. You never ever need {1} because that is the default assumption. Also spaces don't need to be escaped.

Upvotes: 1

Related Questions