dqlxtv1452 2017-02-22 06:52
浏览 85
已采纳

需要一个正则表达式来接受大于0和逗号的数值

i have to check a comma separated value in which i have to allow only numeric character greater than 0 and comma below is format that i required

  • -1,2,3,567,76,67,20,10

and below is the error that should not be accept by the regex

  1. 0,23,344 23,
  2. 0,2323,85 34
  3. 344,44,00
  4. sdfs,34,34,

i have used the regex

this one accept all numeric character and comma but also accept 0

return ( ! preg_match("/^([0-9,])+$/i", $quantity)) ? FALSE : TRUE;

this one accept not accept 0 at any place

return ( ! preg_match("/^([1-9,])+$/i", $quantity)) ? FALSE : TRUE;
  • 写回答

3条回答 默认 最新

  • drsfgwuw61488 2017-02-22 08:26
    关注

    For the sake of good readable and maintainable code I'd rather stick to a validation function - something like this:

    function validateCsv($input)
    {
        foreach (str_getcsv($input) as $value) {
            if (!preg_match('#^[\d]+$#', $value)) {
                throw new \Exception('invalid CSV-value ' . $value);
            }
            if ($value <= 0) {
                throw new \Exception('invalid CSV-value ' . $value);
            }
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法