私はこの正規表現を使用しています:
^\$?([0-9]{1,3},([0-9]{3},)*[0-9]{3}|[0-9]+)(.[0-9][0-9])?$
カンマありまたはなしのドル通貨金額のこの正規表現一致
1000 から 2000 までの数値を通貨形式で照合したい。
例:
マッチ$1,500.00 $2000.0 $1100.20 $1000
一致しない$1,000.0000 $3,000 $2000.1 $4,000 $2500.50
[$]\([1][[:digit:]]\{3\}\|2000\)\(,[[:digit:]]+\|\)
この式はこの言語を定義します:
ドルの後に 1 xyz または 2000 が続きます。
最後に、必要に応じてコンマと 1 つ以上の数字が続きます
どうですか:
/^\$(?:1,?\d{3}(?:\.\d\d?)?|2000(?:\.00?)?)$/
説明:
^ the beginning of the string
----------------------------------------------------------------------
\$ '$'
----------------------------------------------------------------------
(?: group, but do not capture:
----------------------------------------------------------------------
1 '1'
----------------------------------------------------------------------
,? ',' (optional (matching the most amount
possible))
----------------------------------------------------------------------
\d{3} digits (0-9) (3 times)
----------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
----------------------------------------------------------------------
\. '.'
----------------------------------------------------------------------
\d digits (0-9)
----------------------------------------------------------------------
\d? digits (0-9) (optional (matching the
most amount possible))
----------------------------------------------------------------------
)? end of grouping
----------------------------------------------------------------------
| OR
----------------------------------------------------------------------
2000 '2000'
----------------------------------------------------------------------
(?: group, but do not capture (optional
(matching the most amount possible)):
----------------------------------------------------------------------
\. '.'
----------------------------------------------------------------------
0 '0'
----------------------------------------------------------------------
0? '0' (optional (matching the most
amount possible))
----------------------------------------------------------------------
)? end of grouping
----------------------------------------------------------------------
) end of grouping
----------------------------------------------------------------------
$ before an optional \n, and the end of the
string
----------------------------------------------------------------------
) end of grouping