views:

231

answers:

5

I have the following regular expression:

^[-+]?[\d{0,3},?\d{3}]*\.?\d+$

I am trying to support numbers of the following formats:

  • 1
  • -1
  • -1.00
  • 100,000

I am not concerned about scientific notation but my users may or may not enter in commas. The problem I am having is that the expression is matching:

  • 100,
  • 100,00

How can I have the expression indicate that if there is a comma then there must be three characters after it.

+5  A: 

Try this regular expression:

/^[-+]?(?:\d*|\d{1,3}(?:,\d{3})*)(?:\.\d+)?$/
Gumbo
What's the : character do I don't see it my reference.
JoshBerke
@Josh: `(?:…)` is a non-capturing grouping that’s match can not be referenced.
Gumbo
This works for all test cases I've thrown at it except for .25 which we change the first \d+ to \d*.
JoshBerke
@Josh: Yes, using `\d*` instead of `\d+` would match `.25`.
Gumbo
Ok I edited your answer to reflect \d*. Thank you for the help!
JoshBerke
I love magical regexes
CaptnCraig
A: 

You've put your "count" qualifiers inside square brackets, which doesn't make sense (well it makes sense, syntactically, but it's not doing what you think it's doing).

Pointy
+1  A: 

Try this

^([-+]?\d(?:(,\d{3})|(\d*))+\d*)?\.?\d*$ 

SUCCESSES

0
1
-1
-1.00
100,000
100.0
1,111,111
.25

FAILURES

100.0.
100.0,
asdf100.0
100,
1,11,111
,111,111
Chad
That's an odd one. You've build it to accept "100,0000", "10000,000", etc. The only real requirement seems to be that, if there's a comma, then the next 3 characters must be digits -- which is *literally* what the OP asked for, but probably not what he wanted. :-)
Ken
@Ken, good catch, I didn't notice that.
Chad
@Ken: Wow great example of how hard it can be to accuratley specify a simple problem. Probally should have said you need to ensure that there is 1-3 digits before a comma and 3 digits after a comma. I think that covers it.
JoshBerke
+1  A: 

As Robert Harvey indicated, if all you're concerned about is capturing numeric values to use in your program, you can just strip out the commas and all will be well.

However, assuming this is a formatting question (that is, you're checking keystrokes and only allowing valid input, or reformatting the input to be a valid numeric value), then you could try something like this:

EDIT: ^[+-]?\d{1,3}(,\d{3})*(\.\d+)?$

What this does is allow any number of sets of a comma and 3 digits, followed by zero or one set of a dot followed by one or more digits.

Brian Lacy
That last edit was because I tested the regex, fixed the errors, and it does in fact match all the test cases I could think of.
Brian Lacy
[boast] I'd also point out that my regex is, IMO, one of the most straight-forward yet presented. ;D [/boast]
Brian Lacy
It is the easiest to read hence my +1, I'm not sure what the benefit of using a non-capturing vs capturing group which is one of the key diffs btw yours and Gumbo's
JoshBerke
There's no difference except that his version is non-capturing. Mine is easier to read; I don't see the need to add more characters to complicate things, but if you like, feel free to add the ?: inside each group.
Brian Lacy
@Brian, fyi, it fails with ",111". By "fails", I mean passes, and shouldn't.
Chad
A: 

Why not just whack all commas and then test?

g=oldstring.replace(',','');
g=g.replace('.','');//may as well do dots too
if (! g.match('/^[0-9]*[0-9]$/'))
    alert("Bummerific");

Additionally if you're going to allow commas then you shouldn't make them place commas every 3 digits. International users use commas to separate decimal place. In which case if you wanna be pedantic then this regex will probably work

/^[0-9][0-9,.]*[0-9]$/

Good Luck!

flaxeater
Because I can't assume that 100,54.99 wasn't a typo. And let's say hypothetically we are dealing with money here well that could be a very costly typo. As for international users, I can't wait to have enough customers that we start targeting international users and at that point. I will be on a beach sipping my pina-colada:) (Or we can just localize the expression when that day comes).
JoshBerke