4

I am trying to find a piece of regex to match a currency value.

I would like to match only numbers and 1 decimal point ie

Allowed

  • 10
  • 100
  • 100.00

Not Allowed

  • Alpha Characters
  • 100,00
  • +/- 100

I have search and tried quite a few without any luck.

Hope you can advise

1
  • 3
    Note that the comma is the vali character in a lot of country's for decimal places. This software will not pass the turkey test like this. codinghorror.com/blog/archives/001075.html Commented May 2, 2009 at 15:05

4 Answers 4

6
if (preg_match('/^[0-9]+(?:\.[0-9]+)?$/', $subject))
{
    # Successful match
}
else
{
    # Match attempt failed
}

Side note : If you want to restrict how many decimal places you want, you can do something like this :

/^[0-9]+(?:\.[0-9]{1,3})?$/im

So

100.000

will match, whereas

100.0001

wont.

If you need any further help, post a comment.

PS If you can, use the number formatter posted above. Native functions are always better (and faster), otherwise this solution will serve you well.

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

4 Comments

Any reason for the down voting? If you see a genuine problem I'd be happy to tweak the answer. However, down voting without saying anything is just being silly especially when it's the accepted answer.
This would still match 00000, 0.1 and 0.123. And besides the leading zeros, I don’t think Lee wanted a variable number of digits after the decimal point.
@ThePixelDeveloper: why did you add i (case insesitive) and m (multiline) flag? Arent't these useless? You are looking only for digits that are never case sensitive, you ar looking only in one line.
Totally correct @MarcoDemaio. The original post was created 4 years ago and I guess I did things blindly back then. Thanks.
4

How about this

if (preg_match('/^\d+(\.\d{2})?$/', $subject))
{
   // correct currency format
} else {
  //invalid currency format
}

Comments

2

You might want to consider other alternatives to using a regex.

For example, there's the NumberFormatter class, which provides flexible number and currency parsing and formatting, with build in internationalisation support.

It's built into PHP 5.3 and later, and is available as an extension on earlier versions of PHP 5.

Comments

0

Try this regular expression:

^(?:[1-9]\d+|\d)(?:\.\d\d)?$

5 Comments

I tried this. return (preg_match('^(?:[1-9]\d+|\d)(?:\.\d\d)?$', $vale)) ? TRUE : FALSE; And just get error Message: preg_match() [function.preg-match]: No ending delimiter '^' found. Adding it everything passes.
preg_match('/^(?:[1-9]\d+|\d)(?:\.\d\d)?$/')
The preg_* functions use PCRE that need to be surrounded by delimiters. See docs.php.net/manual/en/intro.pcre.php
Still allowing invalid characters. return (preg_match('/^(?:[1-9]\d+|\d)(?:\.\d\d)?$/', $this->price) )? TRUE : FALSE; allows any Alpha character
@Lee: you should test the expression in a simple script (unit test for example): it should work as expected. I don't see how it would allow alpha chars. Actually, it doesn't (I just tested it).

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.