怎么样:
^(?:[0-9]{1,3}(?:,?[0-9]{3})*)?(?:\.[0-9]{0,10})?$
解释:
The regular expression:
(?-imsx:^(?:[0-9]{1,3}(?:,?[0-9]{3})*)?(?:\.[0-9]{0,10})?$)
matches as follows:
NODE EXPLANATION
----------------------------------------------------------------------
(?-imsx: group, but do not capture (case-sensitive)
(with ^ and $ matching normally) (with . not
matching \n) (matching whitespace and #
normally):
----------------------------------------------------------------------
^ the beginning of the string
----------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
----------------------------------------------------------------------
[0-9]{1,3} any character of: '0' to '9' (between 1
and 3 times (matching the most amount
possible))
----------------------------------------------------------------------
(?: group, but do not capture (0 or more
times (matching the most amount
possible)):
----------------------------------------------------------------------
,? ',' (optional (matching the most
amount possible))
----------------------------------------------------------------------
[0-9]{3} any character of: '0' to '9' (3 times)
----------------------------------------------------------------------
)* end of grouping
----------------------------------------------------------------------
)? end of grouping
----------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
----------------------------------------------------------------------
\. '.'
----------------------------------------------------------------------
[0-9]{0,10} any character of: '0' to '9' (between 0
and 10 times (matching the most amount
possible))
----------------------------------------------------------------------
)? end of grouping
----------------------------------------------------------------------
$ before an optional \n, and the end of the
string
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
一些例子:
match : 1,234.45
match : .123
match : 0.12345
match : 123456.7890123
not match : ,123,456.789