doutong2132 2017-11-10 03:56
浏览 46
已采纳

preg_match用于商业信函参考编号

Can you help me to build a better regex for me? I'm now making telegram bot, and it gather user input to check their proposal status. I need to validate their input with preg_match.

example reference number:

11/A-9/XI/2015    
12/A-9/XII/2.99/2015

I'm not doing anything yet, but i consider using preg_match to ensure the input contains some character. Here is my code:

preg_match("#[0-9A-Za-z./]#", $text);

Any better solutions?

  • 写回答

1条回答 默认 最新

  • douzhimao8656 2017-11-10 04:34
    关注

    You could use this based on your string:

    ^[0-9]{1,2}\/[a-zA-Z]-[0-9]\/[A-Z]+\/([0-9.]+\/)?[0-9]+$
    

    It would match 12/A-9/XII/2015 and 12/A-9/XII/2.99/2015 but not a12/A-9/XII/2.99/2015, 12/A-9/XII/a2.99/2015 or 12/A-9/XII/2.99/2015a

    See it live and the explanation here

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

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制