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

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

报告相同问题?