dongpu1879 2016-06-26 07:28
浏览 50
已采纳

为什么使用的分隔符会影响正则表达式的有效性?

I use this pretty simple regular expression:

^[\x20-\x7E]+$

When I try to use it with some of PHP regex functions, such as preg_match() it throws warning in sole case when I use ~ character (tilde) as delimiter.

So, execution of following lines goes well

preg_match("/^[\x20-\x7E]+$/", $s); # delimiter "/"
preg_match("!^[\x20-\x7E]+$!", $s); # delimiter "!"
preg_match("#^[\x20-\x7E]+$#", $s); # delimiter "#"

but for some reason, this line

preg_match("~^[\x20-\x7E]+$~", $s); # delimiter "~"

throws a warning

Warning: preg_match(): Unknown modifier ']' in some_script.php on line XX

note: it happens only when it's used with double-quotes!

I'm using tilde all the time as delimiter and never faced problems with it until this case and really wonder why that happens. Can't find does tilde have some special meaning in regular expressions (i'm 99% now sure it does not), or it's just a bug.

I can certainly work around this, but the question is: What's the difference between tilde and any other delimiter?

  • 写回答

1条回答 默认 最新

  • duanci19881003 2016-06-26 07:37
    关注

    You were using double quotes:

     "~^[\x20-\x7E]+$~"
    

    Which means that both \x20 and \x7E got interpreted in PHP string context, not by PCRE. Guess what \x7E amounts to.

    So as @Bitwise mentioned, use single quotes. Or better yet escape the escape sequences:

     "~^[\\x20-\\x7E]+$~"
    

    Thus the regex engine will still see [\x20-\x7E] instead of [ -~].

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

报告相同问题?

悬赏问题

  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办