两个建议:
preg_match("#^(?:\d{1,3}|1,?\d{3})$#"
说明:
^ # Start of string
(?: # Either match...
\d{1,3} # a three-digit number
| # or
1 # a four digit number that starts with a 1
,? # and may have a thousands separator
\d{3} # (and three more digits)
)
$ # End of string
的问题是,当然,这也让1,999
,所以你还是会需要一个额外的完整性检查。这可能是更好的解决方案。
您可以也不要在正则表达式本身的范围限制,但这是繁琐:
preg_match("#^(?:1,?440|1,?4[0-3]\d|1,?[0-3]\d{2}|[1-9]\d{1,2}|\d)$#"
说明:
^ # Start of string
(?: # Either match...
1,?440 # 1440
| # or
1,?4[0-3]\d # 1400-1439
| # or
1,?[0-3]\d{2} # 1000-1399
| # or
[1-9]\d{1,2} # 10-999
| # or
\d # 0-9
)
$ # End of string
1用于改善整个表达式。 – stema 2012-03-14 08:15:37
从来没有想过做这样 - 这将很好地工作 - 谢谢! – user756659 2012-03-14 09:27:38