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 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突