dongshi6529 2013-09-04 14:07
浏览 47
已采纳

PHP RegEx - 没有结束分隔符? [重复]

This question already has an answer here:

I am fairly new to RegEx and felt I somewhat got the hang of the basics when I was working with my .htaccess file. I am currently working on some form validation to check that the user inputs a valid serial number to the system. The system can accept the following serial format.

  • I-SERIAL-123
  • I-SERIAL123
  • SERIAL-123
  • SERIAL123

I am using the preg_match function to check whether this expression is satisfied, if so the field is submitted.

Current Expression

if (preg_match("^[A-Z0-9\-]{5}$", $_GET['serial']) === false) 

However PHP keeps throwing an "No ending delimiter found" exception, I've looked at a couple of PHP cheat sheets and I don't see any immediate issues in my syntax.

Any pointers would be greatly appreciated, thanks.

</div>
  • 写回答

1条回答 默认 最新

  • dsfgds4215 2013-09-04 14:09
    关注

    You need delimiters around your regex, usually /s:

    if (preg_match("/^[A-Z0-9\-]{5}$/", $_GET['serial']) === false)
                    ^---------------^
    

    But any non-alphanumeric character is valid (even paired brackets), although it makes most sense to use ~, # or other symbols that aren't regex metacharacters or often used in text searches:

    if (preg_match("#^[A-Z0-9\-]{5}$#", $_GET['serial']) === false)
    

    In your case, as pointed out by Andy Lester, the regex engine thinks that ^ was supposed to be the delimiter (possible, but you then lose the "start of string" anchor for use in your regex and have to use \A instead):

    if (preg_match("^\A[A-Z0-9\-]{5}$^", $_GET['serial']) === false)
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器