0

Can anyone give me the regular expression for currency which have the following formats :

1000 - valid
1,000 - valid
1,000.00 or 1000.00 - valid.

This means, the number May or May Not contain a comma(,) separator every 3 digits.

The number May Or May Not contain a dot (.), and if it carries a dot(.) it should show atleast 1 number after the decimal place.

And lastly it should be numerical characters only. If I need to make my question clear kindly suggest.

2
  • I tried without regular expressions. But i have no idea on building regular expressions. Commented Sep 17, 2012 at 7:08
  • 1
    @RanjanSarma: regular-expressions.info Commented Sep 17, 2012 at 7:08

2 Answers 2

3
/^\d{1,3}(?:(?:,\d{3})*|(?:\d{3})*)(?:\.\d{1,2})?$/

"Between one and three digits, then any number of groups of three digits prefixed by a comma or any number of groups of three digits not prefixed by said comma (disallowing a mix of the two kinds of groups), then an optional group of one or two digits prefixed by a dot."

Note: This regex assumes that you want to validate an entire string against the criteria outlined in your question. If you want to use it to find such numbers in a longer string, you will need to remove the ^ and $ from the beginning and end of the expression.

Sign up to request clarification or add additional context in comments.

5 Comments

This will also match "1,000000.00", I doubt this is desirable.
@tdammers: Good point. I have updated the regex to handle that case.
@NamidaAneskans: Kindly update the above expression with the fix for tdammers' bug I need it urgent. Thanking you.
@Ranjan: That's not how it works here. We gladly help you but we don't do your work or fullfil requests. In that case you'd have to hire someone.
@FelixKling: I apologize all of you for that. I am grateful to all of you and especially Namida for his guidance. I just requested him to update it as i am in situation.
1

Something like so should work: (,?\d{3})+(\.\d{2})?. The regex will attempt to match a sequence of 3 digits precedeed by an optional comma, which is then, finally followed by an optional decimal point and 2 digits.

Please refer to this tutorial for more information.

EDIT: As per the comment below, the above regex can fail. I would recommend first using this regular expression: ^[\d][\d.,]+$ to make sure that you only have digits, thousand and decimal seperators. This regular expression will also make sure that the number starts with a digit, not with anything else. You could most likely have one regular expression which does everything, but it will most likely be quite complex.

5 Comments

It would not match 1,000.00 but for example ,999.00.
No, it won't work. This will, for example, match "pizza,123.123pizza!OMG" and ",123123123,123.22", but not "1.50".
@FelixKling: It would match 1,000.00, there is no ^ there.
@tdammers: It should be fixed now.
@FelixKling: It should be fixed now.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.