dsgk0386 2016-10-05 03:10
浏览 229
已采纳

PHP - preg_match - 如何匹配以零开头的数字

I am trying to use preg_match to match numbers that start with a zero to indicate an invalid input. Numbers such as 01, 0023, 00.4 are invalid; however numbers such as 0.12 are valid. The input would be a calculator entry, such as 09 * 108 + 0.19 + 4009. In this case, the expression would be invalid because of the 09.

So far my expression looks like: preg_match("/[^1-9]+0[0-9]+/", $subject)

What I tried to do was match when a zero precedes any string of digits, but is also not preceded by another string of digits. So 109 is valid, but 1+09 is invalid. However it isn't working for cases like 1 + 4009.

  • 写回答

1条回答 默认 最新

  • douzhiba6873 2016-10-05 07:33
    关注
    preg_match('/\b0[0-9]+(?:\.[0-9]+)?\b/', $subject)
    

    \b for word boundary.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 LiBeAs的带隙等于0.997eV,计算阴离子的N和P
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法