Php 货币正则表达式与数值范围匹配
我正在使用这个正则表达式:Php 货币正则表达式与数值范围匹配,php,regex,Php,Regex,我正在使用这个正则表达式: ^\$?([0-9]{1,3},([0-9]{3},)*[0-9]{3}|[0-9]+)(.[0-9][0-9])?$ 此正则表达式与带逗号或不带逗号的美元货币金额匹配 我想用货币格式对1000到2000之间的数字进行匹配 示例: 匹配$1500.00$2000.0$1100.20$1000 不匹配$1000.0000$3000$2000.1$4000$2500.50 [$]\([1][[:digit:]]\{3\}\|2000\)\(,[[:digit:]]+\|
^\$?([0-9]{1,3},([0-9]{3},)*[0-9]{3}|[0-9]+)(.[0-9][0-9])?$
此正则表达式与带逗号或不带逗号的美元货币金额匹配
我想用货币格式对1000到2000之间的数字进行匹配
示例:
匹配$1500.00$2000.0$1100.20$1000
不匹配$1000.0000$3000$2000.1$4000$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
使用正则表达式检查某个数字是否在某个范围内,这是一个很好的例子,说明没有为作业使用正确的工具。将字符串转换为数值,并使用
x>=1000&&x完全同意。如果需要,首先使用regex进行验证,以确保您拥有有效的货币数字,然后从中提取数字,转换为int/float并进行范围检查。