tags:

views:

54

answers:

4

Here's what I currently have, but it only works if the decimal is preceded and followed by a decimal.

^\$?(\d*?(?<=\d)\.?(?=\d)\d*?)$

So the string: '$50.00' matches, but '$.50', and '$50.' don't match (I want them to)

I want to retrieve the matched decimal like '50.00' in a single group if possible so that I can grab the normalized value if there is a match.

A: 
^\$?\s*((?=\d*\.|\d)\d*(?:\.\d*)?)$
Lucero
I tested that with $.1 and it didn't match.
orokusaki
Edited, try again please.
Lucero
That one caught it but it didn't match '1' because you were saying look ahead to see if a decimal follows.
orokusaki
Right. Sorry. Fixed. ;)
Lucero
OK, that's just pimp. The | operator was the number one thing I couldn't figure out. '^\$?\s*((?=\d*\.|\d)\d*(?:\.\d*)?)$' is the master regex for money as far as I'm concerned now. It allows all possible idiotic values (except for commas which make me barf). Thanks Lucero.
orokusaki
This matches `$.` as well as just `.`.
Jay
A: 

Err, what about

^\$?(\d*\.?\d*)$
gregseth
This will also match nothing or just `$`
Lucero
1+ Yours is actually pretty valuable too (does exactly the same thing except I don't want a match on empty because I'm raising errors before evaluating the group(1) result).
orokusaki
+1  A: 

Try this:

^\$?(\d+(?:\.\d*)?|\.\d+)$

It will match:

^\$?           # an optional $ at the begin
  (\d+         # one or more digits
    (?:\.\d*)? # followed by an optional decimal part
  |\.\d+       # or just decimal places without any leading digits
  )$
Gumbo
+1  A: 
(?=^.*\d.*$)^(?:\$\s*)?(?:\d|,)*?\.?(?:\d?)*$

disallows:  
$  
.  
$.
<empty>  
<whitespace>

allows:
$50,000
$500
$0
50,000
500
0
.0
.00000000000
$50,000.000000
$ 5.
Jay