兩個建議:
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